畅通工程
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18811 Accepted Submission(s): 7981
Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
Sample Input
3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
Sample Output
3 ?
Source
套个PRIM模板,然后循环到N-1条边即可,判断此时边是否存在,存在则满足,输出最低成本,不存在则输出 ?
#include <stdio.h> #include <string.h> #define inf 0x6fffff int dis[105]; int map[105][105]; int vis[105]; int sum,n,ok,m; void prim() { int i,j,k,min; for(i=1;i<=n;i++) dis[i]=map[1][i]; vis[1]=1; for(i=1;i<n;i++) //循环到n-1就好 { min=inf; for(j=1;j<=n;j++) { if(vis[j]==0 &&dis[j]<min) { min=dis[j]; k=j; } } if(min==inf) //如果此时有无穷大的边,说明有的路没有联通 { ok=1; break; } vis[k]=1; sum+=min; for(j=1;j<=n;j++) { if(vis[j]==0 &&dis[j]>map[k][j]) dis[j]=map[k][j]; } } } int main() { int i,j,a,l,b; while(scanf("%d%d",&m,&n)!=EOF) { if(m==0) break; for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=inf; for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&l); if(map[a][b]>l) map[a][b]=map[b][a]=l; } ok=0; memset(vis,0,sizeof(vis)); sum=0; prim(); if(!ok) printf("%d\n",sum); else printf("?\n"); } return 0; }
Jungle Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4645 Accepted Submission(s): 3419
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
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
这题也是水题,给你一个图,求最小生成树,就解释下第一行的输入。A 代表A这个地方, 2 代表有B和I两个地方和A相连,距离是分别是12 和25.。
注意输入就好了
#include <stdio.h> #include <string.h> #define inf 0x6fffff int dis[105]; int map[105][105]; int vis[105]; int sum,n; void prim() { int i,j,k,min; for(i=1;i<=n;i++) dis[i]=map[1][i]; vis[1]=1; for(i=1;i<=n;i++) { min=inf; for(j=1;j<=n;j++) { if(vis[j]==0 &&dis[j]<min) { min=dis[j]; k=j; } } if(min==inf) break; vis[k]=1; sum+=min; for(j=1;j<=n;j++) { if(vis[j]==0 &&dis[j]>map[k][j]) dis[j]=map[k][j]; } } } int main() { int i,j,a,l; char b[22],b1[22]; while(scanf("%d",&n)!=EOF&&n!=0) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=inf; for(i=1;i<=n-1;i++) { scanf("%s",b); scanf("%d",&a); for(j=1;j<=a;j++) { scanf("%s",b1); scanf("%d",&l); map[b[0]-'A'+1][b1[0]-'A'+1]=map[b1[0]-'A'+1][b[0]-'A'+1]=l; //我用-‘A‘+1输入,因为我的循环是从一开始的 } getchar(); } memset(vis,0,sizeof(vis)); sum=0; prim(); printf("%d\n",sum); } return 0; }
B - Agri-Net
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
1258
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines
of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
我真的看不出来这道题和HDU 1102有什么区别。。
上代码
#include <stdio.h> #include <string.h> #define inf 0x6fffff int dis[105]; int map[105][105]; int vis[105]; int sum,n; void prim() { int i,j,k,min; for(i=1;i<=n;i++) dis[i]=map[1][i]; vis[1]=1; for(i=1;i<=n;i++) { min=inf; for(j=1;j<=n;j++) { if(vis[j]==0 &&dis[j]<min) { min=dis[j]; k=j; } } if(min==inf) break; vis[k]=1; sum+=min; for(j=1;j<=n;j++) { if(vis[j]==0 &&dis[j]>map[k][j]) dis[j]=map[k][j]; } } } int main() { int i,j,a,b,l; while(~scanf("%d",&n)&&n) { sum=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=inf; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%d",&l); if(map[i][j]>l) map[i][j]=map[j][i]=l; } memset(vis,0,sizeof(vis)); prim(); printf("%d\n",sum); } return 0; }