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

HDOJ Monthly Contest – 2010.04.04

Recommend

lcy

又是最小生成树的裸题

这是代码

#include<stdio.h>
#include<string.h>
#define N 505
#define INF 0x7ffffff
int g[N][N];
int low[N],vis[N];
int n;
int prim()
{
int pos,res=0;
memset(vis,0,sizeof(vis));
vis[1]=1;
pos=1;
for(int i=1;i<=n;i++)
if(i!=pos) low[i]=g[pos][i];
for(int i=1;i<n;i++){
int minn=INF;
pos=-1;
for(int j=1;j<=n;j++)
if(!vis[j] && minn>low[j]){
minn=low[j];
pos=j;
}
if(pos==-1) return -1; //注意这里
res+=minn;
vis[pos]=1;
//printf("**%d %d\n",pos,minn);
for(int j=1;j<=n;j++)
if(!vis[j] && low[j]>g[pos][j])
low[j]=g[pos][j];
}
return res;
}
int main(void)
{
int t,m,k;
int u,v,d;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
g[i][j]=INF;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&d);
if(g[u][v]>d) //notice
g[u][v]=g[v][u]=d;
}
for(int i=0;i<k;i++){
scanf("%d",&d);
scanf("%d",&u);
d--;
while(d--){
scanf("%d",&v);
g[u][v]=g[v][u]=0;
u=v;
}
}
printf("%d\n",prim());
}
return 0;
}

HDU3371--Connect the Cities(最小生成树)的更多相关文章

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

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

  2. hdu3371 Connect the Cities (MST)

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

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

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

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

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

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

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

  6. HDU3371 Connect the Cities

    题目描述: 有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的.现在想把所有的岛都连通起来,求最少的花费是多少. 输入: 第一行输入T,代表多少组数据. ...

  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. Connect the Cities[HDU3371]

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

  9. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  10. Connect the Cities(hdu3371)并查集(附测试数据)

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

随机推荐

  1. ****The Toy of Flandre Scarlet

    The Toy of Flandre Scarlet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

  2. PhotoSwipe简介

    官方网站http://www.photoswipe.com/源码示例http://github.com/downloads/codecomputerlove/PhotoSwipe/code.photo ...

  3. JavaScript里利用DOM获取宽高大全

    网页可见区域宽:document.body.clientWidth网页可见区域高:document.body.clientHeight网页可见区域宽:document.body.offsetWidth ...

  4. window.open实现模式窗口(只弹出一个window.open)

    父页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...

  5. iOS 如何随意的穿插跳跃,push来pop去

    OS 如何随意的穿插跳跃,push来pop去 主题思想:如A.B.C.D 四个视图控制器 想要在 A push B 后, B 在push 到 D ,然后从 D pop 到 C ,在从 C pop 的A ...

  6. 万恶的tileMap

    先吐槽下.. 本来,我们准备用tileMap来做地图的,但发现一个问题,就是tileMap层中不能添加cc.Sprite,这导致了tileMap只适合做2D平面没有遮挡的游戏,并且主角是不能有效率的进 ...

  7. mysql count max min 语句用法

    count 用法 求总条数 $sql="select count(*) as total from e_user"; $query = mysql_query($sql, $lin ...

  8. LogFactory缺包异常

    抛出异常: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/loggin ...

  9. 自动安装Redis服务端与PHP扩展Redis

    该脚本基于阿里云服务器安装脚本,并只能运用于centos / aliyun os,该脚本使用时,需要与阿里云安装脚本的install.sh放在同一目录下.有缘人切忌乱用: #! /bin/bash # ...

  10. Microsoft Visual Studio 2012正式版官方下载

    Microsoft Visual Studio 2012正式版官方下载 首先声明,看到园子里还没有类似的新闻,所以斗胆发首页,如有不妥之处,请移除并谅解. 虽然之前已经又用到泄露的MSDN正式版,但今 ...