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 |
老是wa 找不到错误,望高人指点#include"stdio.h" #include<algorithm> #define maxver 100 #define maxedge 4950 using namespace std; typedef struct{ int u; int v; int w; }Edge; bool cmp(Edge e1,Edge e2){ return e1.w<e2.w; } Edge ed[maxedge]; int p[maxver],r[maxver]; void make_set(int x){ p[x]=x; r[x]=1; } int find_set(int x){ if(x!=p[x]) p[x]=find_set(p[x]); return p[x]; } void union_set(int x,int y){ int i=find_set(x); int j=find_set(y); if(r[i]>r[j]) p[j]=i; else if(r[i]<r[j]) p[i]=j; else{ r[j]++; p[i]=j; } } int kruskal(int n){ int i,sum=0; for(i=1;i<=n;i++) make_set(i); sort(ed+1,ed+n*(n-1)/2+1,cmp); for(i=1;i<n;i++){ if(find_set(ed[i].u)!= find_set(ed[i].v)){ union_set(ed[i].u,ed[i].v); sum+=ed[i].w; } } return sum; } int main(){ int n,q; int i,j,k=1,v1,v2; int w; scanf("%d",&n); for(i=1;i<=n;i++) for(j=1;j<=n;j++){ scanf("%d",&w); if(j>i){ ed[k].u=i; ed[k].v=j; ed[k].w=w; k++; } } scanf("%d",&q); for(i=1;i<=q;i++){ scanf("%d%d",&v1,&v2); k=(v1-1)*n+v2-(v1+1)*v1/2; ed[k].w=0; } printf("%d\n",kruskal(n)); return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator