poj 1861 Network 最小生成树 kruskal
Network
| Time Limit: 1000MS | Memory Limit: 30000K | |||
| Total Submissions: 10959 | Accepted: 4174 | Special Judge | ||
Description
Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.
Input
The
first line of the input contains two integer numbers: N - the number of
hubs in the network (2 <= N <= 1000) and M - the number of
possible hub connections (1 <= M <= 15000). All hubs are numbered
from 1 to N. The following M lines contain information about possible
connections - the numbers of two hubs, which can be connected and the
cable length required to connect them. Length is a positive integer
number that does not exceed 106. There will be no more than
one way to connect two hubs. A hub cannot be connected to itself. There
will always be at least one way to connect all hubs.
Output
Output
first the maximum length of a single cable in your hub connection plan
(the value you should minimize). Then output your plan: first output P -
the number of cables used, then output P pairs of integer numbers -
numbers of hubs connected by the corresponding cable. Separate numbers
by spaces and/or line breaks.
Sample Input
4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1
Sample Output
1 4 1 2 1 3 2 3 3 4
Source
Northeastern Europe 2001, Northern Subregion
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 int n,m; 7 8 struct node 9 { 10 int s,t,w; 11 }edge[15005],ans[1005]; 12 13 int p[1005]; 14 15 int Find(int x) 16 { 17 if (x == p[x])return x; 18 p[x] = Find(p[x]); 19 return p[x]; 20 } 21 22 bool cmp(node a, node b) 23 { 24 if (a.w != b.w)return a.w<b.w; 25 if (a.s != b.s)return a.s<b.s; 26 return a.t<b.t; 27 } 28 29 void kruskal() 30 { 31 int i,k=0; 32 for (i=1; i<=n; i++) 33 p[i] = i; 34 sort(edge,edge+m,cmp); 35 36 for (i=0; i<m; i++) 37 { 38 int x = Find(edge[i].s); 39 int y = Find(edge[i].t); 40 if (x != y) 41 { 42 p[x] = y; 43 ans[++k] = edge[i];//记录结果 44 } 45 if (k == n-1)return; 46 } 47 } 48 49 50 int main() 51 { 52 // freopen("in.txt","r",stdin); 53 int i; 54 while (scanf("%d %d",&n,&m)!=EOF) 55 { 56 for (i=0; i<m; i++) 57 { 58 scanf("%d %d %d",&edge[i].s, &edge[i].t, &edge[i].w); 59 } 60 kruskal(); 61 printf("%d\n%d\n",ans[n-1].w,n-1); 62 for (i=1; i<n; i++) 63 { 64 printf("%d %d\n",ans[i].s,ans[i].t); 65 } 66 } 67 return 0; 68 }
posted on 2013-01-21 21:01 shijianupc 阅读(170) 评论(0) 收藏 举报

浙公网安备 33010602011771号