码迷,mamicode.com
首页 > Web开发 > 详细

POJ 1258 Agri-Net

时间:2014-08-27 20:13:28      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   java   io   for   

Agri-Net

Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1258
64-bit integer IO format: %lld      Java class name: Main
 
 
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

 
解题:最小生成树。。。
 
Kruskal算法
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 10000;
18 struct arc{
19     int u,v,w;
20     arc(int x = 0,int y = 0,int z = 0){
21         u = x;
22         v = y;
23         w = z;
24     }
25 };
26 arc e[maxn];
27 int uf[maxn],n,m;
28 int Find(int x){
29     if(x != uf[x])
30         uf[x] = Find(uf[x]);
31     return uf[x];
32 }
33 bool cmp(const arc &x,const arc &y){
34     return x.w < y.w;
35 }
36 int kruskal(){
37     for(int i = 0; i <= n; i++) uf[i] = i;
38     sort(e,e+m,cmp);
39     int sum = 0;
40     for(int i = 0; i < m; i++){
41         int tx = Find(e[i].u);
42         int ty = Find(e[i].v);
43         if(tx != ty){
44             sum += e[i].w;
45             uf[tx] = ty;
46         }
47     }
48     return sum;
49 }
50 int main() {
51     int i,j,w,u,v;
52     while(~scanf("%d",&n)){
53         for(m = i = 0; i < n; i++){
54             for(j = 0; j < n; j++){
55                 scanf("%d",&w);
56                 if(j > i) e[m++] = arc(i+1,j+1,w);
57             }
58         }
59         printf("%d\n",kruskal());
60     }
61     return 0;
62 }
View Code

 来个优先队列优化的prim。。。哈哈

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 10000;
18 struct arc {
19     int to,w;
20     arc(int x = 0,int y = 0) {
21         to = x;
22         w = y;
23     }
24 };
25 vector<arc>g[maxn];
26 priority_queue< pii, vector< pii >,greater< pii > >q;
27 int n,d[maxn];
28 bool done[maxn];
29 int prim(){
30     for(int i = 0; i <= n; i++){
31         d[i] = INF;
32         done[i] = false;
33     }
34     int ans = d[1] = 0;
35     while(!q.empty()) q.pop();
36     q.push(make_pair(d[1],1));
37     while(!q.empty()){
38         int u = q.top().second;
39         int w = q.top().first;
40         q.pop();
41         if(done[u]) continue;
42         done[u] = true;
43         ans += w;
44         for(int i = 0; i < g[u].size(); i++){
45             if(d[g[u][i].to] > g[u][i].w){
46                 d[g[u][i].to] = g[u][i].w;
47                 q.push(make_pair(g[u][i].w,g[u][i].to));
48             }
49         }
50     }
51     return ans;
52 }
53 int main() {
54     int i,j,w;
55     while(~scanf("%d",&n)) {
56         for(i = 0; i <= n; i++) g[i].clear();
57         for(i = 1; i <= n; i++) {
58             for(j = 1; j <= n; j++) {
59                 scanf("%d",&w);
60                 if(i != j) g[i].push_back(arc(j,w));
61             }
62         }
63         printf("%d\n",prim());
64     }
65     return 0;
66 }
View Code

 

POJ 1258 Agri-Net

标签:des   style   blog   http   color   os   java   io   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3940050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!