标签:style blog http io ar color os sp java
4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1
1 4 1 2 1 3 2 3 3 4
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 }
标签:style blog http io ar color os sp java
原文地址:http://www.cnblogs.com/crackpotisback/p/4111355.html