poj 1258--Agri-Net

Description

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. 

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity 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.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

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

Sample Output

28

接着模板题- -
prim
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define INF 0x7f7f7f7
 4 #define N 110
 5 int map[N][N],dis[N],vis[N];
 6 int n;
 7 int prim()
 8 {
 9     int i,j,now,minn,sum=0;
10     vis[0]=1;
11     for(i=0;i<n-1;i++)
12     {
13         minn=INF;
14         for(j=0;j<n;j++)
15             if(!vis[j]&&dis[j]<minn)
16             {
17                 minn=dis[j];
18                 now=j;
19             }
20         if(minn==INF)
21             return sum;
22         vis[now]=1;
23         sum+=minn;
24         for(j=0;j<n;j++)
25             if(!vis[j]&&map[now][j]<dis[j])
26                 dis[j]=map[now][j];
27     }
28     return sum;
29 }
30 int main()
31 {
32     int i,j;
33     while(scanf("%d",&n)!=EOF)
34     {
35         for(i=0;i<n;i++)
36         {
37             for(j=0;j<n;j++)
38                 scanf("%d",&map[i][j]);
39             dis[i]=map[0][i];
40             vis[i]=0;
41         }
42         printf("%d\n",prim());
43     }
44     return 0;
45 }
kruskal
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 struct eg
 6 {
 7     int u,v,w;
 8 }p[101*101];
 9 int father[101];
10 int n,cnt,sum;
11 bool cmp(eg a,eg b)
12 {
13     return a.w<b.w;
14 }
15 void init()
16 {
17     int i,j,a;
18     for(i=cnt=0;i<n;i++)
19     {
20         for(j=sum=0;j<n;j++)
21         {
22             scanf("%d",&a);
23             if(i>j)
24             {
25                 p[cnt].u=i;
26                 p[cnt].v=j;
27                 p[cnt].w=a;
28                 cnt++;
29             }
30         }
31         father[i]=i;
32     }
33 }
34 int find(int x)
35 {
36     if(x!=father[x])
37         father[x]=find(father[x]);
38     return father[x];
39 }
40 void merge(int x,int y,int z)
41 {
42     x=find(x),y=find(y);
43     if(x==y)
44         return;
45     father[x]=y;
46     sum+=z;
47 }
48 int main()
49 {
50     int i,j;
51     while(scanf("%d",&n)!=EOF)
52     {
53 
54         init();
55         sort(p,p+cnt,cmp);
56         for(i=0;i<cnt;i++)
57             merge(p[i].u,p[i].v,p[i].w);
58         printf("%d\n",sum);
59     }
60     return 0;
61 }

 



posted @ 2013-04-03 12:34  Tamara.c  阅读(169)  评论(0编辑  收藏  举报