Link: HDU 3371
The minimum cost for connecting all cities is known as the number of connected routes and the cost of unconnected routes.
The cost of connected routes is recorded as 0, which is converted into the basic minimum spanning tree.
However, the array should be larger, otherwise it will be easy to re ,,,
#include<cstdio>#include<algorithm>using namespace std;int f[510],n,m;struct stu{ int a,b,c;}t[100000];int cmp(struct stu x,struct stu y){ return x.c<y.c;}int find(int x){ if(x!=f[x]) f[x]=find(f[x]); return f[x];}int krus(){ int i,k=0,s=0,x,y; for(i=1;i<m;i++){ x=find(t[i].a); y=find(t[i].b); if(x!=y){ s+=t[i].c; k++; if(k==n-1) break; f[x]=y; } } if(k!=n-1) s=-1; return s;}int main(){ int N,i,j,k,s,x,y[510]; scanf("%d",&N); while(N--){ scanf("%d%d%d",&n,&m,&k); for(i=1;i<=n;i++) f[i]=i; for(i=1;i<=m;i++) scanf("%d%d%d",&t[i].a,&t[i].b,&t[i].c); m++; while(k--){ scanf("%d",&x); for(i=1;i<=x;i++) scanf("%d",&y[i]); for(i=1;i<=x;i++) for(j=1;j<i;j++){ t[m].a=y[i]; t[m].b=y[j]; t[m++].c=0; } } sort(t+1,t+m,cmp); s=krus(); printf("%d\n",s); } return 0;}