POJ 1251 & HDU 1301 Jungle Roads
题目:
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
题意:
有n个村庄,按字母顺序输入每个村庄的代表字母,其连有k条路,重复的不计。然后输入这k个村庄及这两村庄之间需要的费用。计算最小的费用。
思路:
- 并查集和最小生成树的应用,建立字母和初始化编号间的关系即可解决问题。
代码:
#include <iostream>
#include <algorithm>
#define MAXN 27
using namespace std; int pre[MAXN]; struct node{
int begin;
int end;
int len;
}s[MAXN * (MAXN - )]; bool cmp(node a, node b)
{
return a.len < b.len;
} void init()
{
for(int i = ; i < MAXN; i++)
{
pre[i] = i;
} } int find(int x)
{
while(x != pre[x])
x = pre[x]; return x;
} int merge(int fx, int fy)
{
if(fx != fy)
{
pre[fx] = fy;
return ;
}
else
return ;
} int kruskal(int cnt)
{
int minlen = ;
sort(s, s + cnt, cmp);
for(int i = ; i < cnt; i++)
{
int fx = find(s[i].begin);
int fy = find(s[i].end);
if(merge(fx, fy))
minlen += s[i].len;
}
return minlen;
} int main()
{
char a, b;
int n, k, w;
while(scanf("%d", &n) != EOF)
{
if(n == )
break; init();
int cnt = ;
for(int i = ; i < n-; i++) //n个结点有n-1条路
{
cin >> a >> k;
for(int j= ; j < k; j++)
{
cin >> b >> w;
s[cnt].begin = a - 'A' + ;
s[cnt].end = b - 'A' + ;
s[cnt].len = w;
cnt++;
}
} printf("%d\n", kruskal(cnt));
}
return ;
}
总结:
刚开始用scanf进行输入结果一直没反应超时了,就这样前后又花了好多时间。
- scanf在输入字符类型时容易把空格当作字符读入,总之需要连续输入字符型和整型的话还是避开scanf用cin比较好,因为cin不会出现scanf这样的问题。
POJ 1251 & HDU 1301 Jungle Roads的更多相关文章
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】
题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解 Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- hdu 1301 Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...
- Hdu 1301 Jungle Roads (最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...
- hdu 1301 Jungle Roads krusckal,最小生成树,并查集
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- HDOJ 1301 Jungle Roads
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...
随机推荐
- subprocess.Popen stdout重定向内容实时获取
python 打开一个新进程执行系统命令, test 执行完才能获取返回, test1 实时获取返回结果 import subprocess def test(cmd): p = subprocess ...
- 京东云数据库RDS SQL Server高可用概述
数据库的高可用是指在硬件.软件故障发生时,可以将业务从发生故障的数据库节点迁移至备用节点.本文主要讲述SQL Server高可用方案,以及京东云RDS数据库的高可用实现. 一.高可用解决方案总览 1. ...
- Java之多线程窗口卖票问题(Runnable)
/** * 例子:创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式 * 存在线程的安全问题,待解决. */class Window1 implements Runnable{ p ...
- ubuntu18安装pytorch1.3
环境: ubuntu18 anaconda 创建一个新的环境 conda create -n env_name python=version 激活并进入环境中 conda activate env_n ...
- Djang_框架
- ServletUtils
package com.ruoyi.common.utils; import java.io.IOException; import javax.servlet.http.HttpServletReq ...
- ruoyi IpUtils
package com.ruoyi.common.utils; import java.net.InetAddress; import java.net.UnknownHostException; i ...
- vi几个常用的命令
1.同时打开多个文件:vi 1.txt 2.txt 3.txt 在多个文件中来回切换,命令行模式输入“:next"表示下一个,输入":previous"代表进入上一个,” ...
- Solving ordinary differential equations I(nonstiff problems),exercise 1.1
Solve equation $y'=1-3x+y+x^2+xy$ with another initial value $y(0)=1$. Solve: We solve this by using ...
- oracle中null的理解
< EXAMNO STUNO WRITTENEXAM LABEXAM e2014070001 s25301 80 58 e2014070002 s25302 50 e2014070003 s ...