poj2485

来源:互联网 发布:企业数据安全管理 编辑:程序博客网 时间:2024/06/05 01:20

找最小生成树里面最长的边是多少,还是用kruskal做的。。

 

#include<iostream>#include<algorithm>using namespace std;int father[501];struct n1{int s,e,w;};n1 path[30000];bool cmp(n1 a,n1 b){return a.w<b.w;}int find(int i){while(father[i]!=i){i=father[i];}return i;}int kruskal(int v,int e){int temp1,temp2,i,max1,num_bian;for(i=1;i<=v;i++){father[i]=i;}sort(path+1,path+e+1,cmp);i=max1=num_bian=0;v--;while(num_bian!=v){i++;temp1=find(path[i].s);temp2=find(path[i].e);if(temp1!=temp2){max1=max(path[i].w,max1);num_bian++;if(temp1>temp2){temp1^=temp2^=temp1^=temp2;}father[temp2]=temp1;}}return max1;}int main(){int t,i,j,k,map[501][501],n;scanf("%d",&t);while(t--){scanf("%d",&n);k=0;for(i=1;i<=n;i++)for(j=1;j<=n;j++){scanf("%d",&map[i][j]);if(j>i){k++;path[k].s=i;path[k].e=j;path[k].w=map[i][j];}}printf("%d\n",kruskal(n,k));}}


 

0 0