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 ...
随机推荐
- 用过的关于css的知识
1.代码片段 让两个div并排起来显示. <div style="width:1000px; text-align:center;" id="content&quo ...
- S7-200以太网通信
一.西门子网络系统 二.s7-200通过以太网模块接入以太网 三.S7-200可以接入的以太网系统 四.S7-200以太网通讯实验 五.实验硬件系统组成 六.S7-200作为服务器的配置 1.进入以太 ...
- 第2章KNN算法笔记_函数classify0
<机器学习实战>知识点笔记目录 K-近邻算法(KNN)思想: 1,计算未知样本与所有已知样本的距离 2,按照距离递增排序,选前K个样本(K<20) 3,针对K个样本统计各个分类的出现 ...
- Cookie的简单用法
ASP.NET初学者使用cookie的时候会感觉很陌生,在学习的过程中掌握cookie对象的增删改查非常有必要,,下面是我学习的时候经常用到的这些方法 写入和读取Cookie都需要用户Respone对 ...
- linux 文件权限的基础知识
由于自己总是记不住linux里权限的一些知识,因此简单总结如下: 查看文件权限 // 列出所有文件 ls -al // 最前面的一串10个字母的字符串 // 可能像 drwxrwxr-x // 第一位 ...
- 单独mybatis得使用
今天同学说要学习mybatis后来他写了个程序让我看看,我看了一下发现包引错了,他写的是单独的mybatis,引入的却是spring-mybatis,所以会报错. 今天我记录一下单独mybatis的使 ...
- pyspark进行词频统计并返回topN
Part I:词频统计并返回topN 统计的文本数据: what do you do how do you do how do you do how are you from operator imp ...
- 纯css去实现loading动画效果图
当项目中加载内容慢的的时候,需要显示一个loading动画效果图 之前我们使用的是一圈点点旋转的效果,现在设计修改为,如下gif图片效果-------------------------------- ...
- 自动刷新 CSS文件
自动刷新 CSS文件 使用任何代码工具码 CSS,都是需要保存后再切换到浏览器按 F5 刷新查看效果,一次又一次,不管这个改动仅是一个小小的颜色.使用 CSSrefresh 后,改动 CSS 文件保存 ...
- 欢迎来到Hadoop
What Is Apache Hadoop? Hadoop是一个可靠的.可扩展的.分布式计算的开源软件. Hadoop是一个分布式处理大数据的框架.它被设计成从一台到上千台不等的服务器,每个服务器都提 ...