POJ-1258 Agri-Net(kruskal最小生成树)

Agri-Net

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 60255   Accepted: 24985

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

Source

USACO 102

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 typedef long long LL;
12 const int MAX=105;
13 int n,m;
14 int a[MAX][MAX],fa[MAX*MAX];
15 struct Edge{
16     int u,v;
17     int val;
18     bool operator <(const Edge &xx) const {
19         return val<xx.val;
20     }
21 }edge[MAX*MAX];
22 int getfather(int x){
23     if (fa[x]==x) return x;
24     return fa[x]=getfather(fa[x]);
25 }
26 void init(){
27     int i,j;
28     for (i=1;i<=n;i++){
29         for (j=1;j<=n;j++){
30             scanf("%d",&a[i][j]);
31         }
32     }
33     m=0;
34     for (i=1;i<=n;i++){
35         for (j=i+1;j<=n;j++){
36             edge[++m].u=i;
37             edge[m].v=j;
38             edge[m].val=a[i][j];
39         }
40     }
41     for (i=1;i<=n;i++)
42         fa[i]=i;
43     sort(edge+1,edge+m+1);
44 }
45 int main(){
46     freopen ("net.in","r",stdin);
47     freopen ("net.out","w",stdout);
48     int i,j,ans;
49     while (~scanf("%d",&n)){
50         init();ans=0;
51         int tx,ty;
52         for (i=1;i<=m;i++){
53             tx=getfather(edge[i].u);
54             ty=getfather(edge[i].v);
55             if (tx!=ty){
56                 ans+=edge[i].val;
57                 fa[tx]=ty;
58             }
59         }
60         printf("%d\n",ans);
61     }
62     return 0;
63 }
时间: 2024-11-04 04:41:47

POJ-1258 Agri-Net(kruskal最小生成树)的相关文章

POJ 2485 Highways (kruskal 最小生成树)

Highways POJ 2485so that it will be possible to drive between any pair of towns without leaving the highway system. Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways ca

POJ 1789 Truck History (Kruskal 最小生成树)

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19860   Accepted: 7673 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

POJ 1258 Agri-Net (Prim&amp;Kruskal)

题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复杂度O(n^2) prim: #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; int mat[110][110]

POJ 1258:Agri-Net(最小生成树&amp;amp;&amp;amp;prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38918   Accepted: 15751 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 nee

POJ 1258:Agri-Net(最小生成树&amp;&amp;prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38918   Accepted: 15751 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 nee

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 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 nee

各种最小生成树。 HDU 1863 HDU 1301 POJ 1258

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18811    Accepted Submission(s): 7981 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

poj 1258 Agri-Net(最小生成树果题)

题目链接:http://poj.org/problem?id=1258 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

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 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 nee

POJ 1258 Agri-Net (prim最小生成树)

最小生成树模板题 #include<bits/stdc++.h> using namespace std; int n,a; int dist[120],m[120][120]; void prim() {     bool p[1020];     for(int i=2;i<=n;i++)     {         p[i]=false;         dist[i]=m[1][i];     }     dist[1]=0,p[1]=true;     for(int i=1;