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;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
评论
点赞
收藏
分享
正在热议
# 25届秋招总结 #
440928次浏览 4493人参与
# 春招别灰心,我们一人来一句鼓励 #
41537次浏览 524人参与
# 北方华创开奖 #
107334次浏览 599人参与
# 地方国企笔面经互助 #
7933次浏览 18人参与
# 同bg的你秋招战况如何? #
75751次浏览 552人参与
# 虾皮求职进展汇总 #
114497次浏览 885人参与
# 阿里云管培生offer #
119948次浏览 2219人参与
# 实习,投递多份简历没人回复怎么办 #
2454159次浏览 34849人参与
# 实习必须要去大厂吗? #
55696次浏览 960人参与
# 提前批简历挂麻了怎么办 #
149839次浏览 1977人参与
# 投递实习岗位前的准备 #
1195754次浏览 18547人参与
# 你投递的公司有几家约面了? #
33182次浏览 188人参与
# 双非本科求职如何逆袭 #
661963次浏览 7394人参与
# 如果公司给你放一天假,你会怎么度过? #
4734次浏览 55人参与
# 机械人春招想让哪家公司来捞你? #
157606次浏览 2267人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11402次浏览 275人参与
# 发工资后,你做的第一件事是什么 #
12447次浏览 61人参与
# 工作中,努力重要还是选择重要? #
35638次浏览 384人参与
# 参加完秋招的机械人,还参加春招吗? #
20093次浏览 240人参与
# 我的上岸简历长这样 #
451937次浏览 8088人参与
# 实习想申请秋招offer,能不能argue薪资 #
39248次浏览 314人参与
# 非技术岗是怎么找实习的 #
155855次浏览 2120人参与
牛客网
牛客企业服务