标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28390 | Accepted: 12936 |
Description
Input
Output
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
Hint
Source
不知道是不是我闲的蛋疼……竟然花了十分钟弄了这道超级无敌大水题……好吧,就当是练打字速度吧……
题意:求最小生成树的最大边;
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdlib> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <iostream> 9 #include "algorithm" 10 using namespace std; 11 typedef long long LL; 12 const int MAX=505; 13 int t; 14 int n,m; 15 int a[MAX][MAX]; 16 struct Edge{ 17 int u,v; 18 int w; 19 }edge[MAX*MAX]; 20 int fa[MAX*MAX]; 21 bool cmp(Edge x,Edge y){ 22 return x.w<y.w; 23 } 24 void init(){ 25 int i,j; 26 scanf("%d",&n); 27 for (i=1;i<=n;i++) 28 for (j=1;j<=n;j++) 29 scanf("%d",&a[i][j]); 30 m=0; 31 for (i=1;i<=n;i++) 32 for (j=i+1;j<=n;j++) 33 {edge[++m].u=i; 34 edge[m].v=j; 35 edge[m].w=a[i][j]; 36 } 37 sort(edge+1,edge+m+1,cmp); 38 } 39 int getfather(int x){ 40 if (fa[x]==x) 41 return x; 42 return fa[x]=getfather(fa[x]); 43 } 44 int main(){ 45 freopen ("highways.in","r",stdin); 46 freopen ("highways.out","w",stdout); 47 int i,j; 48 int tx,ty; 49 scanf("%d",&t); 50 while (t--){ 51 init();int ans=0; 52 for (i=1;i<=m;i++) 53 fa[i]=i; 54 for (i=1;i<=m;i++) 55 {tx=getfather(edge[i].u); 56 ty=getfather(edge[i].v); 57 if (tx!=ty) 58 {fa[tx]=ty; 59 ans=max(ans,edge[i].w); 60 } 61 } 62 printf("%d\n",ans);} 63 return 0; 64 }
标签:
原文地址:http://www.cnblogs.com/Michaelzzn/p/5732348.html