博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jungle Roads(最小生成树)
阅读量:3957 次
发布时间:2019-05-24

本文共 3245 字,大约阅读时间需要 10 分钟。

题目:

在这里插入图片描述

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

9A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200

Sample Output

21630

题目描述:

首先输入n代表有n个点,然后输入n-1行,每一行第一个字母和数字m代表这个字母到其他字母有m个路线,后面的是第一个字母到那个字母和具体距离是多少,然后构建一个无向图,求所有点连接起来的最短距离。

注意:每一个字母要以字符串的形式输入,不然会超时(字符串遇到空格会自动结束)。

程序代码:

#include
#include
int e[50][50],dis[1000],book[1000];int main(){
int n,m,i,j,k,t,min; int count,sum,c,b; char s1[5],s[5]; int inf=99999999; while(scanf("%d",&n)!=EOF) {
if(n==0) break; getchar(); memset(e,0,sizeof(e)); memset(dis,0,sizeof(dis)); memset(book,0,sizeof(book)); count=sum=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) {
if(i==j) e[i][j]=0; else e[i][j]=inf; } for(i=1;i
e[j][k]) dis[k]=e[j][k]; } } printf("%d\n",sum); } return 0; }

转载地址:http://nrxzi.baihongyu.com/

你可能感兴趣的文章
简单在单机使用apache-james(开源邮件服务器)
查看>>
lsof 快速起步
查看>>
使用ScribeFire方便地发布blog
查看>>
跨平台Java程序注意事项
查看>>
Python字符与数字的相互转换
查看>>
C 指针解读
查看>>
有关乱码的处理---中国程序员永远无法避免的话题
查看>>
JSP的运行内幕
查看>>
python超简单的web服务器
查看>>
代理模式、静态代理、动态代理、aop
查看>>
Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00
查看>>
大型Web2.0站点构建技术初探
查看>>
机器学习算法汇总:人工神经网络、深度学习及其它
查看>>
解决Spring中AOP不能切入Struts的DispatchAction方法的问题
查看>>
出国以后才知道英语应该怎么学
查看>>
计算机专业权威期刊投稿经验总结
查看>>
如何在三个月内学会一门外语?
查看>>
看看你对Linux到底了解多少?
查看>>
网上看到的:ARM入门最好的文章(转)
查看>>
中国最美情诗100句
查看>>