首页 > 代码库 > POJ 1861 Network

POJ 1861 Network

Network

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

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 61 2 11 3 11 4 22 3 13 4 12 4 1

Sample Output

141 21 32 33 4

Source

Northeastern Europe 2001, Northern Subregion
 
解题:最小生成树。。。题目看起来很唬人啊。。。
 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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 20000;18 struct arc{19     int u,v,w;20     bool operator<(const arc &y) const {21         return w < y.w;22     }23 };24 arc e[maxn];25 int n,m,uf[maxn],ans[maxn],tot;;26 int Find(int x){27     if(x != uf[x]) uf[x] = Find(uf[x]);28     return uf[x];29 }30 int kruskal(){31     for(int i = 0; i < 1500; ++i) uf[i] = i;32     int maxw = tot = 0;33     for(int i = 0; i < m; ++i){34         int tx = Find(e[i].u);35         int ty = Find(e[i].v);36         if(tx != ty){37             uf[tx] = ty;38             ans[tot++] = i;39             maxw = max(maxw,e[i].w);40         }41     }42     return maxw;43 }44 45 int main() {46     while(~scanf("%d %d",&n,&m)){47         for(int i = 0; i < m; ++i)48             scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);49         sort(e,e+m);50         printf("%d\n%d\n",kruskal(),tot);51         for(int i = 0; i < tot; ++i)52             printf("%d %d\n",e[ans[i]].u,e[ans[i]].v);53     }54     return 0;55 }
View Code

 

POJ 1861 Network