Jungle Roads
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

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.

Output

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.

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的更多相关文章

  1. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  2. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  3. 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 ...

  4. Poj(1251),Prim字符的最小生成树

    题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> ...

  5. OpenJudge/Poj 1251 丛林中的路/Jungle Roads

    1.链接地址: http://bailian.openjudge.cn/practice/1251/ http://poj.org/problem?id=1251 2.题目: 总时间限制: 1000m ...

  6. (最小生成树) Jungle Roads -- POJ -- 1251

    链接: http://poj.org/problem?id=1251 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2177 ...

  7. POJ 1251 Jungle Roads (zoj 1406) MST

    传送门: http://poj.org/problem?id=1251 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=406 P ...

  8. POJ 1251 Jungle Roads - C语言 - Kruskal算法

    Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...

  9. poj 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 AC代码: #include<iostream> #include<algor ...

  10. kuangbin_MST A (POJ 1251)

    模板题 Kruskal直接过 调试时候居然在sort(edge + 1, edge + 1 + m)上浪费好多时间... 不过本着ACMer的心态自然要测试一下两种方法分别的速度 Kruskal : ...

随机推荐

  1. 【BZOJ1088】扫雷(递推)

    [BZOJ1088]扫雷(递推) 题面 BZOJ 题解 忽然发现这就是一道逗逼题. 只需要枚举一下第一个是什么,后面都能够推出来了.. #include<iostream> using n ...

  2. 常用Transformation算子

    map 产生的键值对是tupple,      split分隔出来的是数组 一.常用Transformation算子 (map  .flatMap .filter .groupByKey .reduc ...

  3. day5-python基础

  4. php前后端分离项目跨域问题解决办法

    由于之前一直没有做过前后端分离项目,导致走了不少弯路,而且还采用了一种及其不优雅的方法 (在第一次请求的时候把服务器返回的session id保存起来,后续请求的时候把该session id作为参数传 ...

  5. Python内存分配

    一.前言 大多数编译型语言,变量在使用前必须先声明,其中C语言更加苛刻:变量声明必须位于代码块最开始,且在任何其他语句之前.其他语言,想C++和java,允许“随时随地”声明变量,比如,变量声明可以在 ...

  6. 前端PHP入门-032-异常处理-应用级别

    禁止显示错误 在php.ini配置文件中.我们可以控制php的错误显示状态. php.ini中有一个专门的配置项: display_errors 这个选项设置是否将错误信息输出到网页,或者对用户隐藏而 ...

  7. 高可用rabbitmq集群服务部署步骤

    消息队列是非常基础的关键服务,为保证公司队列服务的高可用及负载均衡,现通过如下方式实现: RabbitMQ Cluster + Queue HA + Haproxy + Keepalived 3台ra ...

  8. eclipse常用快捷键大全 (转)

    Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.    ...

  9. [USACO07MAR]黄金阵容均衡Gold Balanced L…

    https://www.luogu.org/problem/show?pid=1360 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many s ...

  10. HDU 1211 EXGCD

    EXGCD的模板水题 RSA算法给你两个大素数p,q定义n=pq,F(n)=(p-1)(q-1) 找一个数e 使得(e⊥F(n)) 实际题目会给你e,p,q计算d,$de \mod F(n) = 1$ ...