POJ 1861 Network
Network
64-bit integer IO format: %lld Java class name: Main
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
Output
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
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 = 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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。