Connect the Cities

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.  

Input

The first line contains the number of test cases. 
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. 

Output

For each case, output the least money you need to take, if it’s impossible, just output -1.

Sample Input

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

Sample Output

1

C++版本一

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;
const int N=510;
int T,t,m,n,k,pre[N],id[N];
struct node{
    int f,t,w;
    node(){};
    node(int a,int b,int c){
        f=a;
        t=b;
        w=c;
    }
    bool operator < (const node &S )const{

        return w<S.w;
    }
}e[N*N];
int find(int x){
    int r=x;
    while(pre[r]!=r)
        r=pre[r];
    return r;
}
void join(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        pre[fx]=fy;
}
int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&k);
        int a,b,c;
        int cnt=0;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            e[++cnt]=node(a,b,c);
        }
        sort(e+1,e+cnt+1);
        for(int i=1;i<=n;i++)
            pre[i]=i;
        for(int K=1;K<=k;K++){
            scanf("%d",&t);
            for(int i=1;i<=t;i++){
                scanf("%d",&id[i]);
                for(int j=1;j<i;j++){
                    join(id[i],id[j]);
                }
            }
        }
        int ans=0;
        for(int i=1;i<=cnt;i++)
            if(find(e[i].f)!=find(e[i].t)){
                join(e[i].f,e[i].t);
                ans+=e[i].w;
            }
        int flag=0;
        for(int i=1;i<=n;i++)
            if (pre[i]==i)
                flag++;
        if(flag==1)
            cout << ans << endl;
        else
            cout << -1 << endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

const int MAX = 25003;

typedef struct Road{
	int x;
	int y;
	int c;
}Road;

Road road[MAX];
int cr;

int pre[501];

void init(int n){
	int i;
	for(i=1;i<=n;++i){
		pre[i] = i;
	}
	cr = n-1;
}

int root(int x){
	if(x!=pre[x]){
		pre[x] = root(pre[x]);
	}
	return pre[x];
}

int merge(int x,int y){
	if(x==-1)return 0;
	int ret = 0;
	int fx = root(x);
	int fy = root(y);
	if(fx!=fy){
		--cr;
		pre[fx] = fy;
		ret = 1;
	}
	return ret;
}

int cmp(const void *a,const void *b){
	return ((Road *)a)->c - ((Road *)b)->c;
}

int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t,n,m,k,cid,tc,pre;
	int sum,i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d %d",&n,&m,&k);
		init(n);
		for(i=0;i<m;++i){
			scanf("%d %d %d",&road[i].x,&road[i].y,&road[i].c);
		}
		sum = 0;
		for(i=0;i<k;++i){
			scanf("%d",&tc);
			pre = -1;
			for(j=0;j<tc;++j){
				scanf("%d",&cid);
				merge(pre,cid);
				pre = cid;
			}
		}
		qsort(road,m,sizeof(Road),cmp);
		for(i=0;i<m;++i){
			if(merge(road[i].x,road[i].y)==1){
				sum += road[i].c;
			}
		}
		if(cr!=0){
			printf("-1\n");
		}else{
			printf("%d\n",sum);
		}
	}
    return 0;
}

C++版本三

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

const int MAX = 25003;

typedef struct Road{
	int x;
	int y;
	int c;
}Road;

Road road[MAX];
int cr;

int pre[501];

void init(int n){
	int i;
	for(i=1;i<=n;++i){
		pre[i] = i;
	}
	cr = n-1;
}

int root(int x){
	if(x!=pre[x]){
		pre[x] = root(pre[x]);
	}
	return pre[x];
}

int merge(int x,int y){
	if(x==-1)return 0;
	int ret = 0;
	int fx = root(x);
	int fy = root(y);
	if(fx!=fy){
		--cr;
		pre[fx] = fy;
		ret = 1;
	}
	return ret;
}

int cmp(const void *a,const void *b){
	return ((Road *)a)->c - ((Road *)b)->c;
}

int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t,n,m,k,cid,tc,pre;
	int sum,i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d %d",&n,&m,&k);
		init(n);
		for(i=0;i<m;++i){
			scanf("%d %d %d",&road[i].x,&road[i].y,&road[i].c);
		}
		sum = 0;
		for(i=0;i<k;++i){
			scanf("%d",&tc);
			pre = -1;
			for(j=0;j<tc;++j){
				scanf("%d",&cid);
				merge(pre,cid);
				pre = cid;
			}
		}
		qsort(road,m,sizeof(Road),cmp);
		for(i=0;i<m;++i){
			if(merge(road[i].x,road[i].y)==1){
				sum += road[i].c;
			}
		}
		if(cr!=0){
			printf("-1\n");
		}else{
			printf("%d\n",sum);
		}
	}
    return 0;
}

 

全部评论

相关推荐

勤奋努力的椰子这就开摆:美团骑手在美团工作没毛病
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
正在热议
# 25届秋招总结 #
443173次浏览 4517人参与
# 春招别灰心,我们一人来一句鼓励 #
42122次浏览 537人参与
# 北方华创开奖 #
107466次浏览 600人参与
# 地方国企笔面经互助 #
7969次浏览 18人参与
# 同bg的你秋招战况如何? #
77083次浏览 569人参与
# 实习必须要去大厂吗? #
55804次浏览 961人参与
# 阿里云管培生offer #
120393次浏览 2220人参与
# 虾皮求职进展汇总 #
116163次浏览 886人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11668次浏览 289人参与
# 实习,投递多份简历没人回复怎么办 #
2454912次浏览 34860人参与
# 提前批简历挂麻了怎么办 #
149924次浏览 1978人参与
# 在找工作求抱抱 #
906075次浏览 9421人参与
# 如果公司给你放一天假,你会怎么度过? #
4762次浏览 55人参与
# 你投递的公司有几家约面了? #
33209次浏览 188人参与
# 投递实习岗位前的准备 #
1196021次浏览 18550人参与
# 机械人春招想让哪家公司来捞你? #
157643次浏览 2267人参与
# 双非本科求职如何逆袭 #
662359次浏览 7397人参与
# 发工资后,你做的第一件事是什么 #
12798次浏览 62人参与
# 工作中,努力重要还是选择重要? #
35896次浏览 384人参与
# 简历中的项目经历要怎么写? #
86935次浏览 1516人参与
# 参加完秋招的机械人,还参加春招吗? #
20148次浏览 240人参与
# 我的上岸简历长这样 #
452049次浏览 8089人参与
牛客网
牛客企业服务