POJ-2485 Highways
作者:互联网
题意:求最小生成树最长边。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include <algorithm> 3 #include <queue> 4 #include <math.h> 5 using namespace std; 6 #define ll long long 7 #define maxx 250005 8 #define inf 0x3f 9 const int mod=1e9+7; 10 //#define int long long 11 struct edge{ 12 int u,v,w; 13 bool operator< (const edge &a)const{ 14 return w<a.w; 15 } 16 }a[maxx];int cnt=0; 17 void add(int u,int v,int w){ 18 a[++cnt].u=u; 19 a[cnt].v=v; 20 a[cnt].w=w; 21 } 22 int n; 23 int fa[maxx]; 24 int find(int x){ 25 return fa[x]==x?x:fa[x]=find(fa[x]); 26 } 27 int krus(){ 28 for(int i=1;i<=n;i++) 29 fa[i]=i; 30 sort(a+1,a+cnt+1); 31 int num=0; 32 for(int i=1;i<=cnt;i++){ 33 int u=find(a[i].u),v=find(a[i].v); 34 if(u!=v){ 35 fa[u]=v; 36 num++; 37 if(num==n-1) 38 return a[i].w; 39 } 40 } 41 } 42 signed main() { 43 int T; 44 scanf("%d",&T); 45 while(T--){ 46 cnt=0; 47 memset(a,0,sizeof a); 48 scanf("%d",&n); 49 for(int i=1;i<=n;i++) 50 for(int j=1;j<=n;j++) { 51 int w; 52 scanf("%d",&w); 53 if(i!=j) 54 add(i,j,w); 55 } 56 printf("%d\n",krus()); 57 } 58 return 0; 59 }View Code
标签:const,int,2485,long,Highways,edge,POJ,include,define 来源: https://www.cnblogs.com/capterlliar/p/15611836.html