Jungle Roads(kruskar)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22633 | Accepted: 10544 |
Description

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.
Input
Output
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30
题解:本来是个水题,结果wa了无数次,最后发现是因为数据有不合法,所以这里吸取一个教训,当输入数据量特别小并且输入字符的时候很容易出现不合法数据,所以这时候最好不要用scanf要用cin更加安全
ac代码;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = ; struct Edge{
int from;
int to;
int w;
bool operator <(const Edge &a) const {
return w<a.w;
}
}edge[];
int fa[N];
int Gf(int x){return (fa[x]==x)?x:fa[x] = Gf(fa[x]);} int cnt;
int n;
int solve()
{
int sum = ;
for(int i = ; i <= N; i++){
fa[i] = i;
}
int tm = ;
for(int i = ; i< cnt; i++){
int X = Gf(edge[i].from);
int Y = Gf(edge[i].to);
if(X!=Y){
sum+=edge[i].w;
tm++;
fa[Y] = X;
if(tm==n-) return sum;
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); //while(~scanf("%d", &n))
while(cin >> n, n)
{
if(n==) return ;
cnt = ;
for(int o = ; o < n-; o++){
//getchar();
char from,to;
//scanf("%c",&from);
cin >> from;
//printf("from = %c\n",from);
int x;
//scanf("%d",&x);
cin >> x; for(int i = ; i< x; i++)
{
//getchar();
int w;
//scanf("%c %d",&to,&w);
cin >> to >> w;
//printf("to = %c\n",to);
edge[cnt].from = from-'A';
edge[cnt].to = to-'A';
edge[cnt++].w = w;
}
}
sort(edge,edge+cnt);
int ans = solve();
//printf("%d\n",ans);
cout << ans << endl;
}
return ;
}
Jungle Roads(kruskar)的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- POJ1251 Jungle Roads 【最小生成树Prim】
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19536 Accepted: 8970 Des ...
- HDU-1301 Jungle Roads(最小生成树[Prim])
Jungle Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- Jungle Roads(最小生成树)
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- (最小生成树)Jungle Roads -- HDU --1301
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1301 http://acm.hust.edu.cn/vjudge/contest/view.action ...
随机推荐
- Bash shell命令记录和CentOS的一些技巧
①CentOS的实用技巧: 一.按下ctrl+alt+F2可由图形界面切换至命令行(shell窗口),按下ctrl+alt+F1可由命令行切换至图形界面(前提是安装CentOS时软件选择项选择安装了图 ...
- 页面重绘(repaint)和回流(reflow)
前言 页面显示到浏览器上的过程: 1.1.生成一个DOM树. 浏览器将获取到的HTML代码解析成1个DOM树,包含了所有标签,包括display:none和动态添加的节点. 1.2.生成样式结构体. ...
- 来腾讯云开发者实验室 学习.NET
腾讯云开发者实验室为开发者提供了一个零门槛的在线实验平台,开发者实验室提供的能力: 零门槛扫码即可免费领取实验机器,支持使用自有机器参与,实验完成后支持保留实验成果: 在线 WEB IDE 支持 sh ...
- java方向及学习方法
随笔:由于回首最近刚刚上班的缘故,平时基本没时间上播客了,所以回首会定期的抽时间分享一些干货给朋友们,就是周期不会像之前那么频繁了.最近有朋友跟回首说想没事儿的时候自学Java,但苦于不知道怎么去学, ...
- KD树
k-d树 在计算机科学里,k-d树( k-维树的缩写)是在k维欧几里德空间组织点的数据结构.k-d树可以使用在多种应用场合,如多维键值搜索(例:范围搜寻及最邻近搜索).k-d树是空间二分树(Binar ...
- python的time模块常用内置函数
1.Python time time()方法 Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数). time()方法语法: time.time() 举例: #! ...
- python的defaultdict
defaultdict是dict的一个子类,接受一个工厂函数作为参数,当访问defaultdict中不存在的key时,会将工厂函数的返回值作为默认的value. class defaultdict(d ...
- [SharePoint]解决用户权限被无缘无故自动删除的问题
前几天在维护公司内网的时候接到了一个case, 说是某个用户的权限无缘无故的就会被SharePoint自动去掉. 刚开始我还不愿意相信这个用户的说法,认为可能是权限赋的方法不对,有可能是被其他人误删了 ...
- Mongodb常规操作【一】
Mongodb是一种比较常见的NOSQL数据库,数据库排名第四,今天介绍一下Net Core 下,常规操作. 首先下C# 版的驱动程序 "MongoDB.Driver",相关依赖包 ...
- ES6 Generators并发
ES6 Generators系列: ES6 Generators基本概念 深入研究ES6 Generators ES6 Generators的异步应用 ES6 Generators并发 如果你已经读过 ...