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. Python9-文件操作-day8

    # 1.文件路径:d:\xxx.txt# 绝对路径:从根目录开始# 想对路径:当前文件下# 2.编码方式:utf-8/gbk...# 3.操作方式:只读.只写.追加.读写.写读..... f=open ...

  2. shell中的$(( )) 的用途:主要用在整数的运算$(( a+b*c ))

    $ a=5; b=7; c=2 $ echo $(( a+b*c ))//注意在进行这些运算时必须是双括号 $ echo $(( (a+b)/c )) $ echo $(( (a*b)%c))

  3. 俯瞰spring

    [简化Java开发] 基于POJO的轻量级和最小入侵性编程: 通过依赖注入和面向接口实现松耦合: 基于切面和惯例进行声明式编程: 通过切面和模板减少样板代码: [容纳你的bean] 容器是spring ...

  4. 使用adb命令启查看已安装的Android应用的versionCode、versionName

    列出已经安装的应用 adb shell pm list package C:\Users\CJTDEV003>adb shell pm list package package:com.sams ...

  5. JMeter安装JSON Path Extractor插件

    下载地址:https://jmeter-plugins.org/wiki/PluginsManager/ 先下载jmeter-plugins-manager-1.3.jar,点击下图中的JAR fil ...

  6. 【转载】CentOS 7安装Python3.5,并与Python2.7兼容并存

    CentOS 7下安装Python3.5 CentOS7默认安装了python2.7.5,当需要使用python3的时候,可以手动下载Python源码后编译安装. 1.安装python3.5可能使用的 ...

  7. 1.ruby基本格式

    1.ruby对于空格是敏感的,很像shell 如:a + b 解释成 a+b(这是两个局部变量) a +b 解释成 a(+b) (这是一个方法调用) 2.在ruby中一行怎么才算结束? Ruby解释分 ...

  8. 【Luogu】P1013进制位(搜索)

    题目链接在这里 这题和虫食算比较类似.做完这道题可以去做虫食算.都是搜索一类的题. 这样 我们分析题目可以发现进制只可能是字母的个数,也就是n-1.为什么? 因为题目要求完整的加法表才算数.如果进制低 ...

  9. ubuntu mysql安装及需要其他主机连服务器mysql时的设置(error:10061)

    说明: 一个朋友在使用ubuntu-server 16.04安装mysql,设置远程访问的时候出现了问题,请我帮忙.但是,我也没有使用过ubuntu安装mysql,于是乎搜索了很多技术文件,比着葫芦画 ...

  10. 【HDOJ6228】Tree(树)

    题意:有一棵n个点的树,在树上的点涂色,每个点涂一种颜色,一共可以涂k种颜色, 然后把同一种颜色(比如说x)的点用最优方案连起来,在连线的边涂上x颜色,问涂k次的边最多有几条 k<=500 si ...