POJ1251 Jungle Roads Kruskal+scanf输入小技巧
Jungle Roads
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 题意:经典最小生成树。
思路:有向边无向边对生成树来说不重要。顺序读入即可。这里有个小技巧,scanf里的空格可以吃掉连续一串(可以为空)空白符(空格,tab,换行),直到出现第一个非空白符,比getchar()更适用。
#include<stdio.h>
#include<algorithm>
using namespace std; int f[],a[];
struct Edge{
int u,v,w;
}edge[]; bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} int kru(int c,int n)
{
int i;
for(i=;i<=n;i++){
f[i]=i;
}
sort(edge+,edge+c+,cmp);
int cnt=,ans=;
for(i=;i<=c;i++){
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=w;
f[fv]=fu;
cnt++;
}
if(cnt==n-) break;
}
if(cnt<n-) return -;
else return ans;
}
int main()
{
int n,x,t,c,i,j;
char ch1,ch2;
while(scanf("%d",&n)&&n!=){
c=;
for(i=;i<n;i++){
scanf(" %c %d",&ch1,&t); //
for(j=;j<=t;j++){
scanf(" %c %d",&ch2,&x); //getchar()个别出现RE情况
edge[++c].u=ch1-'A'+;
edge[c].v=ch2-'A'+;
edge[c].w=x;
}
}
printf("%d\n",kru(c,n));
}
return ;
}
POJ1251 Jungle Roads Kruskal+scanf输入小技巧的更多相关文章
- POJ1251 Jungle Roads(Kruskal)(并查集)
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23882 Accepted: 11193 De ...
- poj1251 Jungle Roads Kruskal算法+并查集
时限: 1000MS 内存限制: 10000K 提交总数: 37001 接受: 17398 描述 热带岛屿拉格里山的首长有个问题.几年前,大量的外援花在了村庄之间的额外道路上.但是丛林不断地超 ...
- HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25993 Accepted: 12181 De ...
- POJ1251 Jungle Roads 【最小生成树Prim】
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19536 Accepted: 8970 Des ...
- POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解
题意: 输入n,然后接下来有n-1行表示边的加边的权值情况.如A 2 B 12 I 25 表示A有两个邻点,B和I,A-B权值是12,A-I权值是25.求连接这棵树的最小权值. 思路: 一开始是在做莫 ...
- poj1251 Jungle Roads(Prime || Kruskal)
题目链接 http://poj.org/problem?id=1251 题意 有n个村庄,村庄之间有道路连接,求一条最短的路径能够连接起所有村庄,输出这条最短路径的长度. 思路 最小生成树问题,使用普 ...
- POJ1251 Jungle Roads【最小生成树】
题意: 首先给你一个图,需要你求出最小生成树,首先输入n个节点,用大写字母表示各节点,接着说有几个点和它相连,然后给出节点与节点之间的权值.拿第二个样例举例:比如有3个节点,然后接下来有3-1行表示了 ...
- HDU1301 Jungle Roads(Kruskal)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- POJ1251 Jungle Roads
解题思路:看懂题意是关键,Kruskal算法,最小生成树模板. 上代码: #include<cstdio> #include<cstring> #include<algo ...
随机推荐
- mysq'l系列之10.mysql优化&权限控制
网站打开慢如何排查 1.打开网页, 用谷歌浏览器F12, 查看network: 哪个加载时间长就优化哪个 2.如果是数据库问题 2.1 查看大体情况 # top # uptime //load av ...
- python使用记录
#2017-7-17 1.用len()函数可以获得list元素的个数; len()可以获取字符串长度 2. list正向0开始索引,,逆向-1开始索引; 也可以把元素插入到指定的位置,比如索引号为1的 ...
- .net概念(转)
你主要想问.Net和Java的差异在哪里 Java是开发语言 .Net叫开发平台 但事实上你管Java叫开发平台也没错 平台就是一个供你在上面进行开发的平台 (英语叫Framework,也可以翻译成“ ...
- 【shell】awk引用外部变量
在使用awk的过程中,经常会需要引用外部变量,但是awk需要使用单引号将print包起来,导致print后的$引用无效,可以采用下面的方式 例如: #!/bin/bash a="line1 ...
- Java for LeetCode 086
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 简单Android代码混淆(转)
代码混淆步骤: 1,project.properties中打开代码混淆功能,加入proguard.config=proguard.cfg 2,编辑proguard.cfg文件,项目没有自动生成时可手工 ...
- 使用vscode写typescript(node.js环境)起手式
动机 一直想把typescript在服务端开发中用起来,主要原因有: javascript很灵活,但记忆力不好的话,的确会让你头疼,看着一月前自己写的代码,一脸茫然. 类型检查有利有敝,但在团队开发中 ...
- HTML5_CSS3仿Google Play垂直菜单
在线演示 本地下载
- Linux_服务器_05_CentOS 7安装完成后初始化的方法_Linux
参考资料 1.CentOS 7安装完成后初始化的方法_Linux
- codeforces 609D D. Gadgets for dollars and pounds(二分+贪心)
题目链接: D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 mega ...