HDU3371--Connect the Cities(最小生成树)
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(最小生成树)的更多相关文章
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu3371 Connect the Cities (MST)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU3371 Connect the Cities
题目描述: 有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的.现在想把所有的岛都连通起来,求最少的花费是多少. 输入: 第一行输入T,代表多少组数据. ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- Connect the Cities(hdu3371)并查集(附测试数据)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- Python调用(运行)外部程序
在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...
- github上前100的ios项目
1. AFNetworking 作者是 NSHipster 的博主, iOS 开发界的大神级人物, 毕业于卡内基·梅隆大学, 开源了许多牛逼的项目, 这个便是其中之一, AFNetworking ...
- EFI Shell 命令说明
EFI Shell 命令说明 引导命令 — EFI Shell 与 nPartition 引导有关的命令. autoboot 设置(查看)自动引导超时变量. bcfg 显示(或修改)驱 ...
- mysql denied for user 'root'@'localhost'
Access[root@log01 ~]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'r ...
- 获取客户端IP地址经纬度所在城市
<?php $getIp=$_SERVER["REMOTE_ADDR"]; echo 'IP:',$getIp; echo '<br/>'; $content = ...
- DOM对象和JQuery有什么不同的地方?
jQuery对象和DOM对象使用说明,需要的朋友可以参考下.1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery ...
- HDU 1532 最大流入门
1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...
- Android的JunitTest
1.在manifest的配置:首先,manifest的下层级中配置: <instrumentation android:name="android.test.Instrumentati ...
- django+nginx+uwsgi 部署配置
django官方文档在这 https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/uwsgi/ 第一步:先收集静态文件 之前要先设置 S ...
- Sublime Text 2
常用功能: 安装Package Control:https://sublime.wbond.net/ 多行选择.多行编辑鼠标选中多行,按下 Ctrl+Shift+L (Command+Shift+L) ...