zoukankan      html  css  js  c++  java
  • USACO3.1.1AgriNet

    Agri-Net
    Russ Cox

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.

    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.

    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.

    The distance between any two farms will not exceed 100,000.

    PROGRAM NAME: agrinet

    INPUT FORMAT

    Line 1: The number of farms, N (3 <= N <= 100).
    Line 2..end: The subsequent lines contain the N x N connectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    SAMPLE INPUT (file agrinet.in)

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    OUTPUT FORMAT

    The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    SAMPLE OUTPUT (file agrinet.out)

    28
    题解:就是求最小生成树。可以用Prim也可以用Kruskal。
    方法一:Prim
    Prim和Dijkstra只有一个区别:Prime是d[v]=min(d[v],w[u][v])而Dijkstra是d[v]=min(d[v],d[u]+w[u][v])。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:agrinet
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 #define MAXS 1000000
     9 long w[105][105];
    10 int d[105];
    11 int visit[105];
    12 int n;
    13 long min,ans;
    14 void init()
    15 {
    16     int i,j;
    17     scanf("%d",&n);
    18     for(i=1; i<=n; i++)
    19         for(j=1; j<=n; j++)
    20             scanf("%ld",&w[i][j]);
    21 }
    22 
    23 void prim()
    24 {
    25     int i,j,k;
    26     d[1]=0;
    27     memset(visit,0,sizeof(visit));
    28     for(i=2; i<=n; i++)
    29         d[i]=w[1][i];
    30     visit[1]=1;
    31     for(i=2; i<=n; i++)
    32     {
    33         min=MAXS;
    34         for(j=2; j<=n; j++)
    35             if(!visit[j]&&d[j]<min)
    36             {
    37                 min=d[j];
    38                 k=j;
    39             }
    40         visit[k]=1;
    41         for(j=2; j<=n; j++)
    42             if(!visit[j]&&w[k][j]<d[j])
    43                 d[j]=w[k][j];
    44     }
    45 
    46 }
    47 void print()
    48 {
    49     int i;
    50     ans=0;
    51     for(i=2; i<=n; i++)
    52         ans+=d[i];
    53     printf("%ld\n",ans);
    54 }
    55 int main(void)
    56 {
    57     freopen("agrinet.in","r",stdin);
    58     freopen("agrinet.out","w",stdout);
    59     init();
    60     prim();
    61     print();
    62     return 0;
    63 }

     方法二:Kruskal

    实现方式就是排序+并查集。我居然把Union写错了。。。

    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:agrinet
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 #define MAXS 1000000
     9 struct edgee
    10 {
    11     long w;
    12     int x;
    13     int y;
    14 }edge[10005],temp;
    15 int f[105];
    16 int n;
    17 long min,ans,total;
    18 void qsort(long l,long r)
    19 {
    20     long i,j,mid;
    21     i=l; j=r; mid=edge[(l+r)>>1].w;
    22     while(i<=j)
    23     {
    24         while(edge[i].w<mid) i++;
    25         while(edge[j].w>mid) j--;
    26         if(i<=j)
    27         {
    28             temp=edge[i];
    29             edge[i]=edge[j];
    30             edge[j]=temp;
    31             i++;
    32             j--;
    33         }
    34     }
    35     if(i<r) qsort(i,r);
    36     if(j>l) qsort(l,j);
    37 }
    38 void init()
    39 {
    40     long i,j,tt;
    41     total=0;
    42     scanf("%d",&n);
    43     for(i=1; i<=n; i++)
    44         for(j=1; j<=n; j++)
    45         {
    46             scanf("%ld",&tt);
    47             if(i<j)
    48             {
    49                 edge[total].w=tt;
    50                 edge[total].x=i;
    51                 edge[total].y=j;
    52                 total++;
    53             }
    54         }
    55 }
    56 int getfather(int x)
    57 {
    58     if(f[x]!=x) f[x]=getfather(f[x]);
    59     return f[x];
    60 }
    61 void kruskal()
    62 {
    63     long i,t,fu,fv;
    64     qsort(0,total-1);
    65     for(i=1; i<=n; i++)
    66         f[i]=i;
    67     ans=0;
    68     i=0;
    69     t=0;
    70     while(t<n-1)
    71     {
    72         fu=getfather(f[edge[i].x]);
    73         fv=getfather(f[edge[i].y]);
    74         if(fu!=fv)
    75         {
    76             ans+=edge[i].w;
    77             t++;
    78             f[fv]=f[fu];
    79 
    80         }
    81         i++;
    82     }
    83     printf("%ld\n",ans);
    84 }
    85 int main(void)
    86 {
    87     freopen("agrinet.in","r",stdin);
    88     freopen("agrinet.out","w",stdout);
    89     init();
    90     kruskal();
    91     return 0;
    92 }

     

  • 相关阅读:
    map按照值排序
    结构体的三种排序方式
    归并排序
    数组模拟双向链表
    二刷斐波那契高精度
    2019年9月训练(贰)区间DP (luogu 4290)
    2019年9月训练(壹)数位DP (HDU 2089)
    2019年8月训练(贰)
    2019年8月训练(壹)二分,三分
    2019年7月训练(柒)
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2913650.html
Copyright © 2011-2022 走看看