hdu1102
将已有边权设为零,再跑kruskal……
#include<bits/stdc++.h> using namespace std; #define int long long const int maxn=50000; struct node{ int x,y,z; }a[maxn]; int n,q,cnt; int fa[maxn]; int cmp(node x,node y){ return x.z<y.z; } int find(int x){ if(fa[x]==x)return x; return fa[x]=find(fa[x]); } signed main(){ while(scanf("%lld",&n)!=EOF){ int x,y; cnt=0; for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ scanf("%lld",&x); a[++cnt]=(node){i,j,x}; } cin>>q; for(int i=1;i<=q;i++){ scanf("%lld%lld",&x,&y); a[(x-1)*n+y].z=0; a[(y-1)*n+x].z=0; //if(find(x)!=find(y)) // fa[find(x)]=find(y); } sort(a+1,a+1+cnt,cmp); int edge=0,tot=0; for(int i=1;i<=cnt;i++){ x=a[i].x,y=a[i].y; if(find(x)!=find(y)){ fa[find(x)]=find(y); tot+=a[i].z; edge++; } if(edge==n-1)break; } printf("%lld\n",tot); } return 0; } /* 3 0 990 692 990 0 179 692 179 0 1 1 2 */