题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3371

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
 /*
问题
输入已经存在的图以及将要增加的边及其花费,计算并输出最小生成树还需要的最小花费 解题思路
将现在的图连起来使得它们的花费为0,再加入将要建设的边,直接跑一边prim即可,注意不能构成最小生成树
的情况说出-1,代码中的u == -1 结束很关键。
*/
#include<cstdio>
#include<cstring> const int N=;
const int INF=;
int e[N+][N+],a[N],dis[N],bk[N];
int prim();
int n,m,k; int main(){
int T,i,j,q,t1,t2,t3,t;
scanf("%d",&T);
while(T--){
for(i=;i<=N;i++){
for(j=;j<=N;j++){
e[i][j] = i==j?:INF;
}
}
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=m;i++){
scanf("%d%d%d",&t1,&t2,&t3);
if(e[t1][t2] > t3){
e[t1][t2] = t3;
e[t2][t1] = t3;
}
}
for(i=;i<=k;i++){
scanf("%d",&t);
for(j=;j<=t;j++){
scanf("%d",&a[j]);
}
for(j=;j<=t-;j++){
for(q=j+;q<=t;q++){
e[ a[j] ][ a[q] ]=;
e[ a[q] ][ a[j] ]=;
}
}
} /*for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
printf("%9d",e[i][j]);
}
printf("\n");
}*/
int ans=prim();
if(ans == -)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
} int prim()
{
int i;
for(i=;i<=n;i++)
dis[i]=e[][i];
memset(bk,,sizeof(bk));
bk[]=;
int c=,sum=,mina,u; while(c < n){
mina=INF;
u=-;
for(i=;i<=n;i++){
if(!bk[i] && dis[i] < mina){
mina=dis[i];
u=i;
}
}
//printf("u==%d\n",u);
if(u == -)
break;
bk[u]=;
c++;
sum += dis[u];
for(i=;i<=n;i++){
if(!bk[i] && dis[i] > e[u][i])
dis[i] = e[u][i];
}
}
if(u == -)
return -;
return sum;
}
 

HDU 3371 Connect the Cities(prim算法)的更多相关文章

  1. hdu 3371 Connect the Cities

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

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

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

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

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

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

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

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

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

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

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

  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 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  9. hdoj 3371 Connect the Cities

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

随机推荐

  1. AngularJS 指令中的require

    require参数可以被设置为字符串或数组,字符串代表另外一个指令的名字.require会将控制器注入到其值所指定的指令中,并作为当前指令的链接函数的第四个参数.字符串或数组元素的值是会在当前指令的作 ...

  2. petaPoco

    petaPoco出现在2011年...因此老鸟可忽略该贴...目前最新版是 5.0, 但核心文件变化不大. 在众多的ORM框架中, 其中不乏非常优秀的EF, 但今天仍然想写点关于PetaPoco的文字 ...

  3. ubuntu下Node.js环境搭建

    Node.js是一个能够在服务器端运行JavaScript的开放源代码.跨平台JavaScript运行环境.Node.js由Node.js基金会持有和维护,并与Linux基金会有合作关系.Node.j ...

  4. Cordova - Windows 下创建第一个 Android App

    官方文档: Create your first Cordova app Android Platform Guide 安装 JDK 和 Android SDK 注意: 需要将 JK 和 Android ...

  5. c#中快速排序的学习

    最近看了一句话,说的是在现实生活中,会写字的人不见得会写出好文章,学习编程语言就像是学会了写字,学会了编程语言并不一定能写出好程序. 我觉得就是很有道理,以前读书的时候,基本学完了C#中的语法知识,算 ...

  6. .net core 使用 AspectCore 实现简易的AopCache。

    (第一次写博客,好紧张!!!) 源码地址:传送门 项目中有很多缓存的需求,能自己定义缓存key和时间,能根据key去清理缓存. 网上找了一圈,有很多基于aop的缓存组件,但是都不满足我的需求.故造了个 ...

  7. 一步一步学习Swift之(三):巧用AutoLayout布局

    一些初学者经常在使用autoLayout时,做得效果不太理想,经常会出现界面错乱的情况. 本文章用一个小实例说明autoLayout的使用 非常的简单,只要记住 规则就可以使界面适屏布局,适配各种ip ...

  8. 【javascript】您好, 您要的ECMAScript6速记套餐到了 (一)

    [前言]本文“严重参考” 自阮一峰老师写的ES6文档,在此我郑重感谢他沉默无声的帮助 总结一下ES6为 javascript中的 对象/数组/函数 这JS三巨头所提供的更简洁优雅的书写方式,以及扩展的 ...

  9. Python 生成器, 迭代器, 可迭代对象的区别

    1.可迭代对象 在python中, 一切皆对象, 但凡是可以用for循环进行遍历取值的对象都可以称之为可迭代对象, 可迭代对象在程序的一个执行周期中,可以无限轮次的进行循环遍历 2.迭代器 a.一个可 ...

  10. react中组件的渲染

    1.封装props对象 2.调用组件函数,得到返回的react元素 3.ReactDom把React元素转成真实的DOM元素并且插入到目标容器内部