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. vmalloc_init

    /* linux/mm/vmalloc.c*/ struct vmap_area { unsigned long va_start; unsigned long va_end; unsigned lo ...

  2. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  3. django的rest framework框架——版本、解析器、序列化

    一.rest framework的版本使用 1.版本可以写在URL中,通过GET传参,如 http://127.0.0.1:8082/api/users/?version=v1 (1)自定义类获取版本 ...

  4. [!] The ‘Pods-项目名XXX' target has frameworks with conflicting names:XXX.framework.

    在集成网易 即时通讯IM时报如下错误: [!] The ‘Pods-Yepu' target has frameworks with conflicting names: nimsdk.framewo ...

  5. pycharm调整字体大小

    问题:pycharm的默认字体比较小,看起来费眼睛 解决办法: 1.打开 file-->settings 2.Editor-->font 注意:没有修改过的需求先保存一下才能修改size

  6. x86保护模式 二 分段管理机制

    分段管理机制 段选择子和偏移地址的二维虚拟地址转换为一维的线性地址 一  段定义和虚拟地址到线性地址的转换 三个参数定义段:段基地址    段界限  和段属性    同时也是段描述符的结构 段基地址为 ...

  7. [BZOJ1596] [Usaco2008 Jan]电话网络(树形DP || 贪心)

    传送门 1.树形DP #include <cstdio> #include <cstring> #include <iostream> #define N 1000 ...

  8. 淘金(bzoj 3131)

    Description 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块.    一阵风吹 ...

  9. 【Codevs1227】方格取数2(费用流)

    题意:给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000) 现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来,该格子的数就变成 ...

  10. Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学

    8493833                 2014-10-31 08:41:26     njczy2010     B - Friends and Presents             G ...