hdu 3371(prim算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10313 Accepted Submission(s):
2937
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 <iostream>
#include <cstdio>
using namespace std; int map[][],r[],Min,n,sum,node[];
const int INF=; void set()
{
for (int i=; i<=n; i++)
{
node[i]=INF;
for (int j=; j<=n; j++)
map[i][j]=INF;
}
} void prim()
{
int tm=,m;
int vis[]= {};
node[tm]=;
vis[tm]=;
for (int k=; k<=n; k++)
{
Min=INF;
for (int i=; i<=n; i++)
if (!vis[i])
{
if (node[i]>map[tm][i])
node[i]=map[tm][i];
if (Min>node[i])
{
Min=node[i];
m=i;
}
}
tm=m;
sum+=Min;
vis[m]=;
}
} int main ()
{
int T;
cin>>T;
while (T--)
{
sum=;
int m,k;
scanf("%d%d%d",&n,&m,&k);
set();
for (int i=; i<=m; i++)
{
int p,q,c;
scanf("%d%d%d",&p,&q,&c);
if (map[p][q]>c)
map[p][q]=map[q][p]=c;
}
while (k--)
{
int t;
scanf("%d",&t);
for (int i=; i<=t; i++)
{
cin>>r[i];
}
for (int i=; i<=t; i++)
{
for (int j=; j<=t; j++)
{
map[r[i]][r[j]]=map[r[j]][r[i]]=;
}
}
}
prim();
if (sum<INF)
printf ("%d\n",sum);
else
printf ("-1\n");
}
return ;
}
hdu 3371(prim算法)的更多相关文章
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1162 Eddy's picture (Kruskal算法,prim算法,最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法.保证联通的线路最短,典型的最小生成 ...
- MST(最小生成树)——Prim算法——HDU 1879-继续畅通工程
Prim算法很好理解,特别是学完了迪杰斯特拉算法之后,更加能理解Prim的算法思想 和迪杰斯特拉算法差不多,由于最后要形成连通图,故任意指定一个点,作为初始点,遍历所有点,以当前最小权值的点(和迪杰斯 ...
- hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)
还是畅通工程 Time Limit: 4000/2 ...
- HDU1102 最小生成树prim算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:给出任意两个城市之间建一条路的时间,给出哪些城市之间已经建好,问最少还要多少时间使所有的城 ...
- 1.1.2最小生成树(Kruskal和Prim算法)
部分内容摘自 勿在浮沙筑高台 http://blog.csdn.net/luoshixian099/article/details/51908175 关于图的几个概念定义: 连通图:在无向图中,若任意 ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 最小生成树のprim算法
Problem A Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
随机推荐
- 腾讯云 activeMQ Illegal character in hostname at index 7
查找问题步骤: 1. /usr/local/apache-activemq-5.9.1/data/activemq.log 看一下这个.log后缀的启动日志,可以将它下载下来再看. 先尝试修改配置文 ...
- union查询
select id, uid, money, FROM_UNIXTIME(created) as created, type FROM ( #type=1是 cjw_finance_bonus ...
- 【TCP】- TCP协议简介
转载:https://blog.csdn.net/ningdaxing1994/article/details/73076795 TCP 是互联网核心协议之一,本文介绍它的基础知识. 一.TCP 协议 ...
- timer实现
实现一个 timer 前段时间写过一篇 blog 谈到 用 timer 驱动游戏 的一个想法.当 timer 被大量使用之后,似乎自己实现一个 timer 比用系统提供的要放心一些.最近在重构以前的代 ...
- zoj3209-Treasure Map
给出一个左下角为\((0,0)\),右上角为\((n,m)\)的矩形,再给出\(k\)个在大矩形内的小矩形(可以重合),问是否能用这些小矩形完全覆盖这个大矩形,并且没有重合,如果可以至少需要多少个. ...
- JS内存空间详细图解
JS内存空间详细图解 变量对象与堆内存 var a = 20; var b = 'abc'; var c = true; var d = { m: 20 } 因为JavaScript具有自动垃圾回收机 ...
- 优先队列实现 大小根堆 解决top k 问题
摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...
- 【题解】51nod 1686第K大区间
成功的秘诀,在于克服自己看题解的冲动……[笑哭].自己A掉这题还是灰常开心的~ 以及爱死 two - pointer ! two - pointer 大法是真的好哇……这个题目有上一题的经验:求第\( ...
- BZOJ4137 & 洛谷4585:[FJOI2015]火星商店问题
https://www.lydsy.com/JudgeOnline/problem.php?id=4137 https://www.luogu.org/problemnew/show/P4585 火星 ...
- bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)
这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...