In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
1
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(i,j,k) for(int i=j; i<k; i++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
const int mod=1e9+7;
struct SD{
int x,y,l;
bool operator <(const struct SD &a)const{
return l<a.l;
}
}a[25010];
char s[2100];
int bk[110],f[5000];
int n,m,k,minn,maxx,ans,cur,sum,x,y;
int nex[4][2]={0,1,1,0,0,-1,-1,0};
int mapp[510][510];
int fin(int x){
return x==f[x]?x:(f[x]=fin(f[x]));
}
int main()
{
int w,t;
cin>>t;
while(t--){
cin>>n>>m>>k;
rep(i,0,m){
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].l);
}
rep(i,1,n+1)f[i]=i;
sort(a,a+m);
rep(i,0,k){
scanf("%d",&sum);
cur=0;
while(sum--){
scanf("%d",&x);
if(!cur)y=x,cur=1;
else{
int xx=fin(x),yy=fin(y);
if(xx!=yy){
f[xx]=yy;
}
}
}
}
ll sum=0;
rep(i,0,m){
int xx=fin(a[i].x),yy=fin(a[i].y);
if(xx!=yy){
f[xx]=yy;
sum+=a[i].l;
}
}
cur=0;
k=fin(1);
rep(i,2,n+1){
if(fin(i)!=k){
cur=1;break;
}
}
if(cur){
cout<<-1<<endl;
}
else cout<<sum<<endl;
}
return 0;
}
//freopen("C:\\Users\\LENOVO\\Desktop\\read.txt","r",stdin);