• tzoj4679 Building Roads(最小生成树+Prim算法)


    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
    总提交: 245            测试通过:79

    描述

     

    Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.

    Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.

    输入

     

    * Line 1: Two space-separated integers: N and M
    * Lines 2..N+1: Two space-separated integers: Xand Yi
    * Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.

    输出

     

    * Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.

    样例输入

    4 1
    1 1
    3 1
    2 3
    4 3
    1 4

    样例输出

     4.00

    题目来源

    USACO 2007 December Silver

     
     
    tzoj卡掉了Kruskal算法,只能交Prim
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int n,m;
     5 const int MAX=1005;
     6 const double INF=0x3f3f3f3f;
     7 struct Point{
     8     double x,y;
     9 }p[MAX];
    10 double g[MAX][MAX];
    11 bool vis[MAX];
    12 double mind[MAX];
    13 
    14 inline double DST(Point a,Point b){
    15     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    16 }
    17 
    18 double prim(){
    19     double res=0,minn;
    20     int k;
    21     vis[1]=true;
    22     for(int i=1;i<=n;i++){
    23         mind[i]=g[1][i];
    24     }
    25     for(int i=1;i<=n-1;i++){
    26          minn=INF;
    27          for(int j=1;j<=n;j++){
    28             if(!vis[j]&&mind[j]<minn){
    29                 minn=mind[j];
    30                 k=j;
    31             }
    32          }
    33          vis[k]=true;
    34          res+=minn;
    35          for(int j=1;j<=n;j++){
    36             if(!vis[j]){
    37                 mind[j]=min(mind[j],g[k][j]);
    38             }
    39          }
    40     }
    41     return res;
    42 }
    43 
    44 int main(){
    45     int u,v;
    46     scanf("%d%d",&n,&m);
    47     for(int i=1;i<=n;i++){
    48         scanf("%lf%lf",&p[i].x,&p[i].y);
    49     }
    50     for(int i=1;i<=n;i++){
    51         for(int j=i+1;j<=n;j++){
    52             g[j][i]=g[i][j]=DST(p[i],p[j]);
    53         }
    54     }
    55     for(int i=0;i<m;i++){
    56         scanf("%d%d",&u,&v);
    57         g[u][v]=g[v][u]=0;
    58     }
    59     printf("%.2f
    ",prim());
    60 }
     
  • 相关阅读:
    Android深度探索--第三章读后感
    Android深度探索--第二章读后感
    Android深度探索--第一章读后感
    android深度探索第十章心得体会
    android深度探索第九章心得体会
    android深度探索第八章心得体会
    深度探索android第七章
    Android 深度探索第六章
    深度探索android第五章
    Android深度探索第四章读后感
  • 原文地址:https://www.cnblogs.com/ChangeG1824/p/11740765.html
Copyright © 2020-2023  润新知