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 : ...
随机推荐
- 菜鸟在线教你用Unity3D开发VR版的Hello World
大家好,我是菜鸟在线的小编.这篇短文将告诉大家如何用Unity3D开发VR版的Hello World. 1开启SteamVR并连接Vive设备 (a)登录Steam客户端,并点击右上角的VR按钮,这时 ...
- python基础----再看property、描述符(__get__,__set__,__delete__)
一.再看property 一个静态属性property ...
- SpringMVC <mvc:view-controller path=""/>标签
<mvc:view-controller path=""/>标签的作用 对应WEB-INF目录下面的JSP页面,我们知道是不能直接使用URL访问到.需要通过转发的方式, ...
- Android Monkey 脚本编写与检查内存泄露
一.Monkey脚本编写 1.Monkey脚本格式 脚本优势: 简单快捷,不需要接触任何工具,只需要一个记事本文件 脚本缺点: 实现坐标.按键等基本操作的相应步骤,顺序脚本无逻辑性 脚本源码: \de ...
- JAX-WS搭建WebService和客户端访问程序
开发环境:myeclipse8.6+jdk1.6.0_29+tomcat6.0.37 XFire搭建webservice: http://www.cnblogs.com/gavinYang/p/352 ...
- zlib解压缩gzip
zlib是个著名的开源解压缩库,gzip是一种压缩文件格式. zlib可以压缩原始数据并输出gzip文件,gzip文件中除了压缩数据外,还有描述这些数据的文件头,所以当原始数据较小时,会出现zlib的 ...
- C语言基本类型的字节数
- ios 虚拟机中文件下载路径
每个人mac上的路径会有不同,你可以打印出你文件下载存放的路径,然后拷贝一下,再单击桌面空白处,最上面的导航栏上有个“前往”,然后找到“前往文件夹”,粘贴一下,就可以找到了.
- 「七天自制PHP框架」应用:Model外键链接
这里以行政区数据为例: 一级行政区数据范例: 二级行政区范例: 三级行政区范例: 在Model层建立三个Model class ProvinceModel extends Model{ public ...
- Spark Core 资源调度与任务调度(standalone client 流程描述)
Spark Core 资源调度与任务调度(standalone client 流程描述) Spark集群启动: 集群启动后,Worker会向Master汇报资源情况(实际上将Worker的资 ...