POJ1251丛林之路题解

  • 题目
    • 链接
    • 字面描述
  • 代码实现

题目

链接

http://poj.org/problem?id=1251

字面描述

Jungle Roads
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 45084 Accepted: 21205
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

Mid-Central USA 2002

代码实现

模板题,不进行思路详解

#include<cstdio>
#include<string.h>
#include<cstring>
using namespace std;const int maxn=100+10;
const int inf=2e9;
int n,ans;
int lowcost[maxn];
int c[maxn][maxn];
bool vis[maxn];
inline void Prim(){//初始化 memset(vis,false,sizeof(vis));ans=0;vis[1]=true;for(int i=1;i<=n;i++)lowcost[i]=c[1][i];//n-1条边的添加 for(int i=1;i<n;i++){int temp=inf;int t=1;//寻找离已添加最近的节点t for(int j=1;j<=n;j++){if(!vis[j]&&lowcost[j]<temp){temp=lowcost[j];t=j; }}ans+=temp;if(t==1)break;vis[t]=true;//更新未添加节点 for(int j=1;j<=n;j++){if(!vis[j]&&c[t][j]<lowcost[j])lowcost[j]=c[t][j];}}
}
int main(){while(1){scanf("%d",&n);if(!n)break;memset(c,0x3f,sizeof(c));for(int i=1;i<n;i++){char op,op2;int k,w;scanf(" %c%d",&op,&k);int u=op-'A'+1;for(int j=1;j<=k;j++){scanf(" %c%d",&op2,&w);int v=op2-'A'+1;c[u][v]=w;c[v][u]=w;} }Prim();printf("%d\n",ans);}return 0;
}

POJ1251丛林之路题解相关推荐

  1. 花-寒-进-13-A-丛林之路

    丛林之路 题目 最小生成树–prim算法 #include <iostream> #include <string.h> using namespace std; const ...

  2. hdu 3812 Sea Sky 深搜+剪枝

    题目意思: 从sea 找一条道sky 的最长路 题解: 先进行一系列的预处理 1.将所有不重复的字符串先保留下来,然后从小到大排序,这样深搜的时候最先弄出来的答案就是最小的 字母序. 2. 进行深度优 ...

  3. [洛谷P2384]最短路

    题目大意:给你一个图,要你求出其中1->n路径中乘积最小的一条路 题解:用$log_2$把乘法变成加法,然后记录每个点的前驱,最后求出答案 C++ Code: #include<cstdi ...

  4. jzyz 1225 调查干草

    描述 Description 奶牛用完了干草,这是一件必须马上补救的可怕事情. Bessie打算看N (2 <= N <=2,000)个农场(标号为1-N)去调查干草的情况.她将走过一些或 ...

  5. (转贴) 《超越自己》

    第1部分 01 -------------------------------------------------------------------------------- 一个娇生惯养,从未出过 ...

  6. 算法训练营 图的应用(最小生成树)

    最小生成树 子图:从原图中选中一些由节点和边组成的图,称之为原图的子图. 生成子图:选中一些由边和所有节点组成的图,称之为原图的生成子图. 生成树:如果生成的子图恰好是一棵树,则称之为生成树. 最小生 ...

  7. 龙应台与安德烈:爱的教育

    有名作家龙应台曾说过:"所谓父女母子一场,只不外意味着,你和他的缘分就是此生当代不时地在目送他的背影渐行渐远.你站立在巷子的这一端,看着他逐步消逝在巷子转弯的中央,并且,他用背影默默通知你: ...

  8. 浙江理工大学 我的编程之路 零基础学C/C++ 200题 标程/题解

    浙江理工大学 我的编程之路 零基础学C/C++ 200题 标程/题解 OJ地址:http://47.96.116.66/index.php 标程/题解GitHub:https://github.com ...

  9. 浙江理工大学c语言作业网站,浙江理工大学 我的编程之路 零基础学C/C++ 200题 标程/题解...

    浙江理工大学 我的编程之路 零基础学C/C++ 200题 标程/题解 OJ地址:http://47.96.116.66/index.php 标程/题解GitHub:https://github.com ...

最新文章

  1. HttpWebRequest在GetResponse时总是超时
  2. TokuDB在生产环境的应用场景(zabbix也可以)
  3. 九、Spark模块和安装
  4. ai如何做倒角和圆角_石材路沿石是如何倒角的?倒角费用是多少?路沿石质量标准?...
  5. ffmpeg to webm
  6. Debug enterprise search menu
  7. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch
  8. python下载大文件mp4_python下载mp4 同步和异步下载支持断点续下
  9. 重磅快讯:CCF发布最新版推荐中文科技期刊目录
  10. 我的一篇思想汇报——君子务本,本立而道生
  11. c 语言 00字符串 截断,c语言截断字符串
  12. [二分][贪心]JZOJ P3996 Sabotage
  13. Oracle 初始化参数文件pfile和spfile
  14. discuz自动添加兼容html5标签的音乐播放器
  15. 一键修改电脑硬件信息_不想使用iPhone锁屏键?一键锁屏快捷指令来了
  16. python源码剖析_Python源码剖析的作品目录
  17. js基于后台数据实现table行列合并
  18. 《安富莱嵌入式周报》第228期:2021.08.30--2021.09.05
  19. python大作业数据_python 爬虫初探和简单数据分析及可视化,帮学妹写个大作业...
  20. tomcat部署web应用及架设论坛

热门文章

  1. 产品经理02_竞品分析
  2. iOS 驾驭 StoryBoard
  3. 迪威视讯打造全国网格化管理样板 龙华“织网工程”
  4. 海龟交易法则 | 稳定性并不等于低风险!
  5. 当在浏览器地址栏输入一个URL后回车,将会发生的事情?
  6. 常见的通讯协议总结(USART、IIC、SPI、485、CAN)
  7. 铁路巡检及指挥调度系统
  8. 详解 502 Bad Gateway nginx/xxx
  9. python实现视频播放器_对目前的视频播放器不满意?教你用Python做一个视频播放器...
  10. 7-7 社交集群 (30 分) (集合数组的方法)