hdu 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.
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
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.
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
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
30
30
Prim算法 促进理解了最小生成树的构建 找边
代码:
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std; #define INF 0x3f3f3f3f;
int cost[][],mincost[],ans,cnt;
int n,m;
bool vis[]; void prim()
{
memset(vis,false,sizeof(vis));
memset(mincost,0x3f,sizeof(mincost));
ans=cnt=mincost[]=;
while(true){
m=;
for(int i=; i<=n; i++){
if(!vis[i] && mincost[i]<mincost[m])
m=i;
}
if(m==)
break;
vis[m]=true;
ans+=mincost[m];
for(int i=; i<=n; i++){
mincost[i]=min(mincost[i],cost[m][i]);
}
}
} int main()
{
int t,s1,s2;
char a[],b[];
while(~scanf("%d",&n),n){
memset(cost,0x3f,sizeof(cost));
for(int i=; i<n; i++){
scanf("%s %d",a,&t);
s1=a[]-'A'+;
for(int j=; j<t; j++){
scanf("%s%d",b,&s2);
cost[s1][b[]-'A'+]=cost[b[]-'A'+][s1]=s2;
}
}
prim();
printf("%d\n",ans);
}
}
hdu Jungle Roads(最小生成树)的更多相关文章
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- HDU 1301Jungle Roads(最小生成树 prim,输入比较特殊)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1301 Jungle Roads Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- hdu1301 Jungle Roads 最小生成树
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- hduoj-1301 Jungle Roads(最小生成树-克鲁斯卡尔和普里姆求解)
普里姆求解: #include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- (最小生成树)Jungle Roads -- HDU --1301
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1301 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
随机推荐
- 给Ubuntu安装KDE桌面 [转]
自ubuntu5.10发布以来,我非常难得的让她在我的硬盘上顽强生存了近3个月,仔细想想就连当年我第一个接触的红旗Linux也不过坚持了一个月,呵呵,又写了一段废话── 所 谓的“重回Kununtu” ...
- 设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称
转载请注明出处:http://blog.csdn.net/u012860063 问题:设单链表中存放n个字符.试设计一个算法,使用栈推断该字符串是否中心对称,如xyzzyx即为中心对称字符串. 代码例 ...
- 人人API 分享到人人功能 修改版
最近在搞一个日程管理网站, 需要实现分享到人人功能, 所以找了一下人人API, 然后根据自己需要修改了一下. 首先得有一个人人给的js文件, 如下: var Renren = Renren || {} ...
- Java SE学习之数组——匿名数组和不规则数组
本文是学习网络上的文章时的总结以及自己的一点实践.感谢大家无私的分享. 近期偶然遇到了数组的问题,学习了匿名数组和不规则数组. 匿名数组适用于仅仅使用一次的情况:不规则数组适用是每行数据总数不确定的情 ...
- JavaScript的"类"
1. 基本创建“类”方式 var Class = function(){ var klass = function(){ this.init.apply(this, arguments); }; kl ...
- ng-repeat出现环路输出Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique
采用ng-repeat循环发生错误时,如下面的输出对象: Duplicates in a repeater are not allowed. Use 'track by' expression to ...
- 右键菜单中的好友列表Ajax直接跳转请求到登陆页面
今天,我们正在做正确的菜单.当点击重命名Ajax要求,并且不发送数据的背景,但直接跳到主页. 我百思不得其解,后来我发现在头版的一个问题: <li><a href='#' oncli ...
- Swing JDialog监听回车键
在做项目时,发现在JDialog中,直接通过addKeyListener来监听回车键不起作用,无法监听到回车键,后面在网上查了些资料,终于解决了.方法如下: KeyStroke stroke = Ke ...
- 聪明的kk
聪明的kk 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 聪明的"KK" 非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘.体现出本 ...
- KindEditor参数具体解释
width 编辑器的宽度.能够设置px或%.比textarea输入框样式表宽度优先度高. 数据类型: String 默认值: textarea输入框的宽度 演示样例: K.create('#id', ...