Jungle Roads + 最小生成树算法+ 并查集
来源:互联网 发布:大数据分析师 编辑:程序博客网 时间:2024/06/16 14:30
Jungle Roads
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 4
Problem Description
![](http://acm.hdu.edu.cn/data/images/1301-1.gif)
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
/*思路:最小生成树算大(prime算法) */#include<iostream>#include<cstdio>using namespace std ;#define MAX 505#define inf 1000000000int s[MAX][MAX] , vi[MAX] , n;/*void prime() //用prime算法计算最小生成树距离{int i , j , sum = 0 ;while(true){int min1 = inf , k =- 1;for( i = 1 ; i <= n ; i ++){if(vi[i]){for( j = 1 ; j <= n ; j ++)if(!vi[j] && min1 > s[i][j]){k = j ;min1 = s[i][j] ;}}}if( k == - 1)break;vi[k] = 1;sum += min1 ;}int cnt = 0 ;for( i = 1 ; i <= n ; i ++)if(vi[i])cnt ++ ;if(cnt == n && sum < inf )//cout << sum << endl ;printf("%d\n",sum);else//cout << "-1" << endl ;printf("-1\n");return ;}*/void prime(){int i,j,k,min,dist[MAX],sum=0;for(i=1;i<=n;i++)dist[i]=s[1][i];vi[1]=1;for(i=2;i<=n;i++){min=inf;k=i;for(j=1;j<=n;j++)if(!vi[j]&&dist[j]<min){min=dist[j];k=j;}sum=sum+min;if(sum>=inf)break;vi[k]=1;for(j=1;j<=n;j++)if(!vi[j]&&s[k][j]<dist[j])dist[j]=s[k][j];}if(sum>=inf)printf("-1\n");elseprintf("%d\n",sum);}int main(void){while( cin >> n && n){int i , j ;memset(vi , 0 , sizeof(vi));for( i = 0 ; i < MAX ; i ++) //初始化每条路无穷大for( j = 0 ; j < MAX ; j ++)s[i][j] = s[j][i] = inf ;for( i = 0 ; i < n-1 ; i ++){char ch;int t ;cin >> ch >> t;for(j = 0 ; j < t ; j ++){char ch1 ;int cost ;cin >> ch1 >> cost ;s[ch-'A'+1][ch1-'A'+1] = s[ch1-'A'+1][ch-'A'+1] =s[ch-'A'+1][ch1-'A'+1] > cost ? cost : s[ch-'A'+1][ch1-'A'+1] ;}}prime();}return 0 ;}
并查集:
#include<stdio.h>#include<algorithm>using namespace std;int father[105];typedef struct node{int a,b;int c;}NODE;NODE ca[5005];bool cmp(NODE x,NODE y){if(x.c<y.c){return 1;}return 0;}int findset(int x){return x!=father[x]?father[x]=findset(father[x]):x;}int kruskal(int m){int i,mincost=0,x,y;for(i=0;i<m;i++){x=ca[i].a;y=ca[i].b;x=findset(x);y=findset(y);if(x!=y){father[x]=y;mincost+=ca[i].c;}}return mincost;}int main(){int n,m,i,mincost=0;while(scanf("%d",&m)&& m ){n = 0 ;for(i=0;i<m-1;i++){getchar();char ch ;int l ;//cin >>ch >> l;scanf("%c %d",&ch,&l);while(l--) //保存每个节点的值{getchar();char ch1 ;int cost ;//cin >> ch1 >> cost ;scanf("%c %d",&ch1 , &cost);ca[n].a = ch -'A' + 1 ;ca[n].b = ch1 - 'A' + 1;ca[n++].c = cost ;}}for(i=0;i<m;i++){father[i]=i;}sort(ca,ca+n,cmp);mincost=kruskal(n);int count=0;for(i=0;i<m;i++){if(father[i]==i){count++;}}if(count>1){puts("-1\n");}else{printf("%d\n",mincost);}}return 0;}
- Jungle Roads + 最小生成树算法+ 并查集
- Jungle Roads(最小生成树+并查集)
- Jungle Roads(并查集+最小生成树)
- Jungle Roads(最小生成树+并查集)
- POJ - 1251 Jungle Roads (最小生成树Kruskal、并查集)
- 九度王道考研机试真题 2012-北大Jungle Roads 并查集 最小生成树
- Jungle Roads解题分析(并查集,最小生成树,输入格式)
- Jungle Roads(最小生成树)
- Jungle Roads 最小生成树
- hdu 1301 Jungle Roads(最小生成树Prim算法)
- hdu1301 Jungle Roads(Prime算法求最小生成树)
- POJ-1251-Jungle Roads-最小生成树-Kruskal算法
- poj1251 Jungle Roads 最小生成树,kruskal算法
- poj 1251 Jungle Roads(最小生成树,普里姆算法)
- HDU : 1301 ( Jungle Roads ) 最小生成树mst prim算法
- HDU 1301 Jungle Roads 最小生成树K算法
- HDU - 1301 - Jungle Roads (最小生成树!!prim算法!!)
- HDU 1301 Jungle Roads prim算法 最小生成树
- substring在java中的用法
- 淘宝爆款的误区,怎么合力打造爆款流量
- 面向过程的程序设计语言学习要点
- NYOJ 298 点的变换 (矩阵快速幂)
- 为什么在直方图画布上画出直方图用了减法
- Jungle Roads + 最小生成树算法+ 并查集
- DataTable.Compute方法使用实例
- DataTable.Compute()方法学习
- linux arm openssl与libusb编译,备忘
- IIS7.5
- 游戏开发工具之纹理打包器-2.打开文件夹遍历指定格式的图片,以及显示到tree控件上
- POJ1039 Pipe
- http://acm.hdu.edu.cn/showproblem.php?pid=1722
- android:shape的使用