Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
y放代码在这里#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxm 100005 #define maxn 1005 #define min(a,b) a<b?a:b using namespace std; struct Edge { int u,v,w; }edge[maxm]; int n,m; int max,p[maxn]; int find(int x) { return p[x]==x?x:p[x]=find(p[x]); } bool cmp(struct Edge a,struct Edge b) { return a.w>b.w; } void init() { for(int i=0;i<n;i++) p[i]=i; } int kruskal() { int i; sort(edge,edge+m,cmp); int tot=0,ans=99999999; init(); for(i=0;i<m&&tot<n-1;i++) { int x=find(edge[i].u),y=find(edge[i].v); if(x!=y) { ans=min(ans,edge[i].w); tot++; p[x]=y; } } return ans; } int main() { int i,t,num=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=0;i<m;i++) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); printf("Scenario #%d:\n",num++); printf("%d",kruskal()); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator