hdoj 3371 Connect the Cities
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12903 Accepted Submission(s):
3549
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.
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.
if it’s impossible, just output -1.
#include<stdio.h>
#include<string.h>
#define MAX 550
#define INF 0x3f3f3f
int city;
int visit[MAX],map[MAX][MAX],low[MAX];
void prime()
{
int j,i,min,mincost=0,next;
memset(visit,0,sizeof(visit));
for(i=1;i<=city;i++)
{
low[i]=map[1][i];
}
visit[1]=1;
for(i=1;i<city;i++)
{
min=INF;
for(j=1;j<=city;j++)
{
if(!visit[j]&&min>low[j])
{
next=j;
min=low[j];
}
}
if(min==INF)
{
printf("-1\n");
return ;
}
mincost+=min;
visit[next]=1;
for(j=1;j<=city;j++)
{
if(!visit[j]&&low[j]>map[next][j])
low[j]=map[next][j];
}
}
printf("%d\n",mincost);
}
int main()
{
int n,m,j,i,k,q,p,c,t,x,y,l;
int a[MAX];
scanf("%d",&n);
while(n--)
{
scanf("%d%d%d",&city,&m,&k);
for(i=1;i<=city;i++)
{
for(j=1;j<=city;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=INF;
}
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&p,&q,&c);
if(map[p][q]>c)
map[p][q]=map[q][p]=c;
}
while(k--)
{
scanf("%d",&t);
for(l=1;l<=t;l++)
scanf("%d",&a[l]);
for(i=1;i<t;i++)
{
for(j=i+1;j<=t;j++)
{
map[a[i]][a[j]]=map[a[j]][a[i]]=0;
}
}
}
prime();
}
return 0;
}
hdoj 3371 Connect the Cities的更多相关文章
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- 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 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
随机推荐
- PowerDesigner15(16)在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
1.用PowerDesigner15建模,在Database—>Generate Database (或者用Ctrl+G快捷键)来生产sql语句,却提示“Generation aborted d ...
- 安卓从业者应该关注:Android 6.0的运行时权限
Android 6.0,代号棉花糖,自发布伊始,其主要的特征运行时权限就很受关注.因为这一特征不仅改善了用户对于应用的使用体验,还使得应用开发者在实践开发中需要做出改变. 没有深入了解运行时权限的开发 ...
- 苹果p12文件--一个苹果证书怎么多次使用(蛋疼,这些问题只有和其他企业合作才会遇到,别人的账号不可能给你,蛋疼....)
在苹果开发者网站申请的证书,是授权mac设备的开发或者发布的证书,这意味着一个设备对应一个证书,但是99美元账号只允许生成3个发布证书,两个开发证书,这满足不了多mac设备的使用,使用p12文件可以解 ...
- 强强合体:Docker版Kali Linux发布
Kali Linux是一款开源的基于Debian的渗透测试专用操作系统,系统中包含一系列用于渗透测试的神器.最近,Kali的开发者们为喜爱Docker的童鞋们发布了新版本. FreeBuf百科:什么是 ...
- VS下遇到未能加载文件或程序集 错误
这个的错误原因可能是在64的系统上编译32位的应用程序,遇到这个错误,可以通过下面的手段解决! 1.关闭Visual Studio. 2. 在Visual Studio Tools子目录,以管理员身份 ...
- bochs编译安装
1.安装依赖sudo apt-get install gtk2.0-devsudo apt-get install xorg-dev 2.配置 ./configure --enable-debugge ...
- poj2182
首先容易知道,最后一个数是最容易确定的,于是从后往前确定 对于位置j,它的数就是1~n中剩余数的第a[j]+1小的数 这当然可以用平衡数做,复杂度为O(nlogn) 有没有更简洁得算法?树状数组+二分 ...
- Unity3D集成SVN进行版本控制
首先,AssetServer确实很好用,Unity内部集成的管理界面,操作很简单,提交冲突的后还可以进行文件比对.但学习使用过程中,发现文件体积较大的项目文件目录(600M),我提交不上去,会返回没有 ...
- PHP分次读取xls
<?php $sourceFile = "1.tmp"; //要下载的临时文件名 $outFile = "用户订单.xls"; //下载保存到客户端的文件 ...
- C#字符串与char数组互转!
字符串转换成Char数组string str="abcdefghijklmnopqretuvwxyz";char[] chars=str.ToCharArray(); char数组 ...