标签:
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<algorithm> #include<queue> #include<vector> #include<ctype.h> #define LL __int64 using namespace std; const int MAXN=100000+5; const int MAX=1000+5; struct node { int s; int e; int val; bool operator<(const node A) const { return val<A.val; } }a[MAXN],b[MAX]; int p[MAX],ans,cnt; int n,m,q; int findfa(int x) { p[x]==x?x:p[x]=findfa(p[x]); } int kru() { int res=0; sort(a,a+m); for(int i=1;i<=n;i++) p[i]=i; for(int i=0;i<m;i++) { int x=findfa(a[i].s); int y=findfa(a[i].e); if(x!=y) { p[x]=y; b[cnt++]=a[i]; res+=a[i].val; } } return res; } int kru1(node now) { int res=0; cnt=0; b[n-1]=now; sort(b,b+n); for(int i=1;i<=n;i++) p[i]=i; for(int i=0;i<n;i++) { int x=findfa(b[i].s); int y=findfa(b[i].e); if(x!=y) { p[x]=y; b[cnt++]=b[i]; res+=b[i].val; } } return res; } int main() { while(scanf("%d %d %d",&n,&m,&q)!=EOF) { for(int i=0;i<m;i++) scanf("%d %d %d",&a[i].s,&a[i].e,&a[i].val); cnt=0; kru(); while(q--) { node now; scanf("%d %d %d",&now.s,&now.e,&now.val); ans=kru1(now); printf("%d\n",ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/sola1994/p/4440669.html