poj 1251 Jungle Roads

来源:互联网 发布:无锡源石数据招聘骗局 编辑:程序博客网 时间:2024/05/22 11:46

Jungle Roads

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 45   Accepted Submission(s) : 34
Problem 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.

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.

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-1行,每一行第一个字母代表一个村庄,然后后面那个数代表要连的村庄的个数,然后输入要连的村庄和对应的距离,输入0代表输入结束!让求最短的距离能将各个村庄都联系起来!
思路:
从边的角度出发进行考虑,题目离散的将点和距离都告诉了我们,我们就要将他们都建立一个联系(通过结构体),然后把边都建立起来之后,就只剩下用最小生成树的方法求解就行了!
代码:
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;int n,m;char pre[105];//注意这是字符型的,就这一点最容易出错! void init(){for(char i='A';i<='Z';i++)pre[i]=i;}struct node {int u,v,w;}p[10005];int find(int x)//找根节点 {int r=x;while(r!=pre[r]){r=pre[r];}int i,j;i=x;while(i!=r){j=pre[i];pre[i]=r;i=j;}return r;}int join(int x,int y)//连到一起 {int fx=find(x);int fy=find(y);if(fx!=fy)//判断是否成环! {pre[fx]=fy;return 1;}return 0;}int cmp(node a,node b){return a.w<b.w;}int main(){char c,ch;int a,t;while(scanf("%d",&n)&&n){init();t=0;for(int i=1;i<n;i++){            getchar();// 注意吸收回车! scanf("%c",&c);scanf(" %d",&m);for(int j=1;j<=m;j++)//建立边! {getchar();//吸收回车! t++;scanf("%c %d",&ch,&a);p[t].u=c;p[t].v=ch;p[t].w=a;}}sort(p+1,p+t+1,cmp);int sum=0;for(int i=1;i<=t;i++){if(join(p[i].u,p[i].v)){sum+=p[i].w;}}printf("%d\n",sum);}return 0;}


再贴一个代码,原来那个在poj上运行超时,但在杭电上不超时,然后讨论区说不能加getchar(),这个就是不要getchar()的代码:
 
 
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int n,m;int pre[105];struct node {int u,v,w;}p[10005];void init(){for(int i=1;i<=n;i++){pre[i]=i;}}int cmp(node a,node b){return a.w<b.w;}int find(int x){int r=x;while(r!=pre[r]){r=pre[r];}int i,j;i=x;while(i!=r){j=pre[i];pre[i]=r;i=j;}return r;}int join(int x,int y){int fx=find(x);int fy=find(y);if(fx!=fy){pre[fx]=fy;return 1;}return 0;}int main(){char c,ch;int d,t;while(scanf("%d",&n)&&n){    init();//别忘啦初始化! t=0;for(int i=1;i<n;i++)//我感觉这种做法很奇怪,换行的时候对应的是代码上的空格,不理解!!!!!! {//这样就只不过少执行了一条语句罢了! scanf(" %c %d",&c,&m);//记住不能有换行符,应该前面有空格! for(int j=1;j<=m;j++){getchar();scanf(" %c %d",&ch,&d);t++;p[t].u=c-'A'+1;//将字符都转化为数字!这样不容易出错! p[t].v=ch-'A'+1;p[t].w=d;}}sort(p+1,p+t+1,cmp);int sum=0;for(int i=1;i<=t;i++){if(join(p[i].u,p[i].v)){sum+=p[i].w;}}printf("%d\n",sum);}return 0;}

刚学过prim,用prim算法写个代码:
 
//prim算法! #include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define INF 0x3f3f3f3fint n,m;int d[105][105];int vis[105];int dis[105];void init(){char c,ch;int a,u,v;memset(d,INF,sizeof(d));memset(vis,0,sizeof(vis));for(int i=1;i<n;i++){//getchar();scanf(" %c %d",&c,&m);for(int j=1;j<=m;j++){getchar();scanf("%c %d",&ch,&a);u=c-'A'+1;//这一点不能用c继续保存它的值,因为它将会重复减字符A,变成不是你想要的值! v=ch-'A'+1;//应该用另一个变量来保存临时值 d[u][v]=d[v][u]=a;}}vis[1]=1;for(int i=1;i<=n;i++){dis[i]=d[1][i];}dis[1]=0;}void prim(){int min,k;for(int i=1;i<n;i++){min=INF;k=1;for(int j=1;j<=n;j++){if(!vis[j]&&(dis[j]<min)){min=dis[j];k=j;}}vis[k]=1;for(int j=1;j<=n;j++){if(!vis[j]&&(dis[j]>d[k][j])){dis[j]=d[k][j];}}}int sum=0;for(int i=1;i<=n;i++){sum+=dis[i];}printf("%d\n",sum);}int main(){while(scanf("%d",&n)&&n){init();prim();}return 0;}

在这里面,我再谢谢求这类题目的做法吧!
 
其实,我感觉这类题,如果人家直接给过你两点之间的距离的话,你用prim算法比较容易,但是给你两个点,及两点之间的距离的话,还是用克鲁斯卡尔的方法做,我感觉克鲁斯卡尔的方法不容易出错,所以一般建议还是用克鲁斯卡尔的方法做!
 
二者的区别:
 
 

克鲁斯卡尔(Kruskal)算法因为只与边相关,则适合求稀疏图的最小生成树。而prime算法因为只与顶点有关,所以适合求稠密图的最小生成树。

 

无疑,Kruskal算法在效率上要比Prim算法快,因为Kruskal只需要对权重边做一次排序,而Prim算法则需要做多次排序。尽管Prim算法每次做的算法涉及的权重边不一定会涵盖连通图中的所有边,但是随着所使用的排序算法的效率的提高,Kruskal算法和Prim算法之间的差异将会清晰的显性出来!

 

 

 


1 0
原创粉丝点击