Connect the Cities

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 13299 Accepted Submission(s): 3618

Problem Description
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
Author
dandelion
Source
Recommend
lcy | We have carefully selected several similar problems for you:

pid=1301" target="_blank">
1301
2544 1598 2066

pid=1874" target="_blank">
1874


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct graph{
int a;
int b;
int cost;
}G[ 25005];
int root[505];
int n,m;
int find(int i){
if(root[i]==i) return i;
return root[i]=find(root[i]);
}
void unio(int i,int j){
int t=find(i);
int k=find(j);
if(t<=k) root[k]=t;
else root[t]=k;
return ;
}
int cmp(graph u,graph v){
return u.cost<v.cost;
}
int shortest;
void kruskal(){
sort(G,G+m,cmp);
int i,t,k;
shortest=0;
for(i=0;i<m;++i){
t=find(G[i].a); k=find(G[i].b);
if(t!=k){
unio(t,k);
shortest+=G[i].cost;
}
}
return ;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int k,i,j,p,q,c,t,x,y;
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=n;++i) root[i] = i;
for(i=0;i<m;++i){
scanf("%d%d%d",&p,&q,&c);
G[i].a=p,G[i].b=q,G[i].cost=c;
}
for(i=1;i<=k;++i){
scanf("%d",&t);
scanf("%d%d",&x,&y);
unio(x,y);
for(j=3;j<=t;++j){
scanf("%d",&y);
unio(x,y);
} }
kruskal();
int nu=0;
for(i=1;i<=n;++i){
if(find(i)==i) nu++;
}
if(nu>1) printf("-1\n");
else printf("%d\n",shortest); }
return 0;
}

hdoj-3371-Connect the Cities【最小生成树】的更多相关文章

  1. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  2. hdoj 3371 Connect the Cities

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. POJ:3371 Connect the Cities(最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...

  4. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  5. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

  6. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  7. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  9. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  10. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

随机推荐

  1. invalid LOC header (bad signature)

    [产生原因] 本地maven仓库相关jar存在问题. [解决方案] 删除本地maven相关jar并重新下载.

  2. Makefile学习(一)----初步理解

    一.我对makefile的理解: 经过一段时间对makefile的学习,我理解的makefile就是将程序员手动编译源文件的过程用一个脚本执行,这对于小型项目来说,程序员手动执行和用makefile来 ...

  3. nw335 debian sid x86-64 -- 6 第三方驱动

    nw335 debian sid x86-64 -- 6 第三方驱动

  4. .NET重构(二):ArrayList,List,IList的联系和区别

    导读:在机房重构的时候,为了降低耦合,不能返回DataTable型数据,而需要转换为泛型集合.我一直使用的就是IList,那天师傅过来帮我挑错,问我:你为什么一直写IList呢,不应该是List吗?好 ...

  5. IIS中如何应用程序启用https协议

    首先已经安装完了SSL证书 1.找到需要添加的站点,右击 ---> 编辑绑定-->添加--->选择  ""https"-->选择"SSL ...

  6. LINQ-查询表达式基础

    一.LINQ查询的数据源 从应用程序的角度来看,原始源数据的特定类型和结构并不重要. 应用程序始终将源数据视为 IEnumerable<T> 或 IQueryable<T> 集 ...

  7. 九度oj 题目1252:回文子串

    题目描述: 输入一个字符串,输出该字符串中对称的子字符串的最大长度. 比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. 输入: 存在多组数据,每组数据一行字 ...

  8. BZOJ 1294 [SCOI2009]围豆豆Bean ——计算几何

    显然我们不可能表示出一台路径,因为实在是太复杂了. 所以我们可以记录一下路径对答案的影响,显然路径对答案影响相同的时候,答案更优,所以我们可以用影响来代替路径. 所以我们考虑状压一下所有的豆子有没有被 ...

  9. spring两个核心IOC、AOP

    Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Java 开发框架,由 ...

  10. 【2018.2.8-】网络流学习笔记(含ISAP!)

    网络流的基础内容就不详细发了,网上到处都是,可自学. 总版点这里 ps:以下有些链接是hihocoder的题目(题面有详细讲解),请确保先登录hihocoder,再点击进入相应题目网页. 最大流 基础 ...