POJ - 1251
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 20024 | Accepted: 9234 |
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
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.
Output
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.
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
Source
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
#define maxn 110
#define INF 1000000
using namespace std;
int n;
int Edge[maxn][maxn];
int path[maxn];
int vis[maxn];
int sum = ;
void prime(int u0)
{
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
path[i] = Edge[u0][i];
}
vis[u0] = ;
sum = ;
for(int i=; i<=n; i++)
{
int tt,mmin = INF;
for(int j=; j<=n; j++)
{
if(vis[j] == && path[j] < mmin)
{
mmin = path[j];
tt = j;
}
}
if(mmin == INF) break;
sum += path[tt];
vis[tt] = ;
for(int k=; k<=n; k++)
{
if(vis[k] == && path[k] > Edge[tt][k])
{
path[k] = Edge[tt][k];
}
}
}
printf("%d\n",sum);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(~scanf("%d",&n))
{
if(n == ) break;
char ch,ch1;
int u,v;
int w,Q;
memset(Edge,INF,sizeof(Edge));
memset(path,INF,sizeof(path));
for(int i=; i<n; i++)
{
cin>>ch1>>Q;
u = ch1 - ;
for(int j=; j<=Q; j++)
{
cin>>ch1>>w;
v = ch1 - ;
Edge[u][v] = w;
Edge[v][u] = w;
}
}
prime();
}
return ;
}
POJ - 1251的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- Poj(1251),Prim字符的最小生成树
题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> ...
- OpenJudge/Poj 1251 丛林中的路/Jungle Roads
1.链接地址: http://bailian.openjudge.cn/practice/1251/ http://poj.org/problem?id=1251 2.题目: 总时间限制: 1000m ...
- (最小生成树) Jungle Roads -- POJ -- 1251
链接: http://poj.org/problem?id=1251 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2177 ...
- POJ 1251 Jungle Roads (zoj 1406) MST
传送门: http://poj.org/problem?id=1251 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=406 P ...
- POJ 1251 Jungle Roads - C语言 - Kruskal算法
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- poj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 AC代码: #include<iostream> #include<algor ...
- kuangbin_MST A (POJ 1251)
模板题 Kruskal直接过 调试时候居然在sort(edge + 1, edge + 1 + m)上浪费好多时间... 不过本着ACMer的心态自然要测试一下两种方法分别的速度 Kruskal : ...
随机推荐
- git用户名和邮箱配置
1. 设置全局用户名和邮箱 git config --global user.name "xxx" git config --global user.email "xxx ...
- 【bzoj2006】【NOI2015】超级钢琴
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4292 Solved: 2195[Submit][Statu ...
- [ACM][2018南京预赛]Magical Girl Haze
一.题面 样例输入: 15 6 11 2 21 3 42 4 33 4 13 5 64 5 2 样例输出: 3 二.思路 关键词:分层BFS 考试时觉得题干意思很清晰——求可将k条边赋值为0的最短路. ...
- 相机标定 和 单应性矩阵H
求解相机参数的过程就称之为相机标定. 1.相机模型中的四个平面坐标系: 1.1图像像素坐标系(u,v) 以像素为单位,是以图像的左上方为原点的图像坐标系: 1.2图像物理坐标系(也叫像平面坐标系)(x ...
- poppo大根堆的原理与实现。
大根堆的定义:1 大根堆是一个大根树 2 大根堆是一个完全二叉树 所以大根堆用数组表示是连续的,不会出现空白字段. 对于大根堆的插入 对于大根堆的插入,可以在排序前确定大根堆的形状,可以确定元素5从位 ...
- Xshell连接Linux服务器总掉线
Xshell连接linux服务器总掉线,解决办法如下: 1.登录服务器后 [root@test134 ~]# cd /etc/ssh/ [root@test134 ssh]# vim sshd_con ...
- 3.6 scikit-learn:Python中的机器学习
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...
- Linux下如何卸载软件(Debian系)
说明:此方法适用于Debian.Ubuntu等带apt工具的操作系统. 步骤: 1.首先我们需要知道将要卸载的软件名称,比如我现在打算卸载tightvncserver,但是如果你不确定名称,没关系,可 ...
- JAVA中反射机制四
声明:如需转载请说明地址来源:http://www.cnblogs.com/pony1223 反射四 利用反射获取类的属性 1.通过反射也可以获取到类中的属性,假设我们继续使用Person这个类,然后 ...
- ④ 设计模式的艺术-04.抽象工厂(Abstract Factory)模式
抽象工厂模式 用来生产不同产品族的全部产品.(对于增加新的产品,无能为力:支持增加产品族) 抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种.业务分类时,通过抽象工厂模式产生需要的对象是一种非常 ...