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. javaweb之session过期验证

    session过期判断的基本思想:用户登录成功后,将用户账号信息保存在session中,然后几乎每次执行命令都要经过过滤器,过滤器检查session中是否存在账号,若不存在, 则返回登录页面,反之正常 ...

  2. reinterpret_cast应用

    reinterpret_cast 的一个实际用途是在哈希函数中,即,通过让两个不同的值几乎不以相同的索引结尾的方式将值映射到索引. #include <iostream> using na ...

  3. 图片的 base64 编码

    图片的 base64 编码就是将一幅图片编码成一串字符串,使用该字符串代替图像地址.我们所看到的网页上的图片,都是需要消耗一个 http 请求下载而来的:(所有才有了 csssprites 技术< ...

  4. APICloud界面间跳转

    UZModule提供属性controller,可通过该控制器对目标控制器进行push或者present操作. // push[self.viewController.navigationControl ...

  5. java学习 (2)xml操作 SAX(增、删、改、查)

    sax是事件驱动的,sax是一种推模式 SAX常用事件: startDocument()----文档开始事件 startElement()-----元素开始事件 charElement()----文本 ...

  6. linux 文件和目录操作

    1 相对路径和绝对路径 (与C++中概念相似) cd /root/cat 绝对路径 cd ../文件名/ 相对路径,指相对某个文件而言的 2 切换目录 cd  当前目录 进入某个目录 pwd 显示当前 ...

  7. javascript Navigator对象

    0.Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 1.对象属性 2. var browser=navigator.appName; var b_version=n ...

  8. POJ 1328 Radar Installation#贪心(坐标几何题)

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...

  9. Python之深浅拷贝&函数

    一.深浅拷贝 深浅拷贝是指copy模块下的copy()和deepcopy()方法. 1.浅拷贝 示例: >>> import copy >>> a = 'hello ...

  10. luci编译错误

    make[3]: Entering directory '/home/hbg/test1214/package/feeds/luci/luci'*** Repository layout change ...