poj 1251
来源:互联网 发布:csmhuan实时数据 编辑:程序博客网 时间:2024/05/21 10:08
Jungle Roads
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 15866 Accepted: 7121
Description
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
自己写的模板,测模板~
这是一道纯最小生成树的题目;
代码:
View Code
1 #include<iostream> 2 #include<string.h> 3 using namespace std; 4 #define INF 100000000; 5 int a[100][100]; 6 int vispoint[100]; 7 int p[100]; 8 int main(){ 9 int n;10 while(cin>>n){11 if(n==0)12 break;13 memset(vispoint,-1,sizeof(vispoint));14 memset(a,-1,sizeof(a));15 int start;16 int sum=0;17 int flag=0;18 for(int i=0;i<n-1;i++){19 char temp;20 int times;21 cin>>temp>>times;22 for(int j=0;j<times;j++){23 if(flag==0)24 start=temp-'A'+1;25 char temp2;26 int temp3;27 cin>>temp2>>temp3;28 int tem1,tem2,tem3;29 tem1=temp-'A'+1;30 tem2=temp2-'A'+1;31 tem3=temp3;32 a[tem1][tem2]=temp3;33 a[tem2][tem1]=temp3;34 }35 }36 int times=0;37 p[times++]=start;38 vispoint[start]=1;39 for(int g=0;g<n-1;g++){40 int point=0;41 int minn=INF;42 for(int k=0;k<times;k++){43 for(int i=1;i<=n;i++){44 if(a[p[k]][i]!=-1&&vispoint[i]==-1&&a[p[k]][i]<minn){45 minn=a[p[k]][i];46 point=i;47 }48 }49 } 50 sum+=minn;51 if(vispoint[point]==-1){52 vispoint[point]=1;53 p[times++]=point;54 }55 }56 cout<<sum<<endl;57 }58 return 0;59 }
外加用邻接表些的代码:
代码:
View Code
1 #include<iostream> 2 using namespace std; 3 #define INF 1000000000 4 int first[100]; 5 struct node{ 6 int start; 7 int end; 8 int w; 9 int next;10 };11 node ver[100];12 int visver[100];13 int a[100];14 int vispoint[100];15 int main(){16 int n,m;17 while(cin>>n>>m){18 memset(first,-1,sizeof(first));19 memset(ver,-1,sizeof(ver));20 memset(visver,-1,sizeof(visver));21 memset(vispoint,-1,sizeof(vispoint));22 for(int i=1;i<=m;i++){23 int temp1,temp2,temp3;24 cin>>temp1>>temp2>>temp3;25 if(first[temp1]==-1){26 first[temp1]=i;27 }28 else{29 int temp=first[temp1];30 first[temp1]=i;31 ver[i].next=temp;32 }33 ver[i].start=temp1;34 ver[i].end=temp2;35 ver[i].w=temp3;36 if(first[temp2]==-1){37 first[temp2]=i+n;38 }39 else{40 int temp=first[temp2];41 first[temp2]=i+n;42 ver[i+n].next=temp;43 }44 ver[i+n].start=temp2;45 ver[i+n].end=temp1;46 ver[i+n].w=temp3;47 }48 int times=0;49 int start=1;50 a[times++]=start;51 vispoint[start]=1;52 int sum=0;53 int flag=0;54 for(int j=1;j<n;j++){55 int minn=INF;56 int p=0;57 for(int k=0;k<times;k++){58 for(int g=first[a[k]];g!=-1;g=ver[g].next){59 // cout<<g<<" "<<ver[g].next<<" "<<ver[g].end<<" "<<ver[g].w<<endl;60 if(ver[g].w<minn&&vispoint[ver[g].end]==-1){61 minn=ver[g].w;62 p=g;63 }64 }65 }66 // cout<<minn<<" "<<p<<" "<<ver[p].end<<" "<<times<<endl;67 if(minn>=INF){68 flag=1;69 break;70 }71 sum+=minn;72 visver[p]=1;73 if(vispoint[ver[p].end]==-1){74 a[times++]=ver[p].end;75 vispoint[ver[p].end]=1;76 }77 }78 if(flag==1){79 cout<<"NONE"<<endl;80 continue;81 }82 cout<<sum<<endl;83 for(int i=1;i<=m+m;i++){84 if(visver[i]==1){85 cout<<ver[i].start<<" "<<ver[i].end<<" "<<ver[i].w<<endl;86 }87 } 88 }89 return 0;90 } 91
- poj 1251
- POJ 1251
- poj 1251
- poj-1251
- poj 1251
- poj 1251
- POJ 1251
- poj 1251
- POJ 1251
- poj-1251
- poj 1251
- POJ 1251
- POJ 1251
- POJ 1251 Jungle Roads
- poj 1251 Jungle Roads
- poj 1251 Prim算法
- poj 1251 Jungle Roads
- POJ-1251 jungle roads
- 最大流模板 Ford-Fulkerson and Dinic
- usaco 1.1 Broken Necklace
- usaco 1.1 Miking Cows
- usaco 1.1 Trasformations
- codeforce 11 11 A B
- poj 1251
- 次小生成树 (poj 1679)
- usaco 29 name that number
- usaco 1.2 Palindromic Squares
- Training@USC Greedy Tino
- usaco 1.2 76 mixing milk 90 Dual Palindromes
- lua table to string,string to table
- usaco 24 barn repair
- 搜索 poj 1416