POJ2263 Heavy Cargo

                                                                                         Heavy Cargo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2412 Accepted: 1321

Description

Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive.

Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.

Input

The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.

Output

For each test case, print three lines:
  • a line saying "Scenario #x" where x is the number of the test case
  • a line saying "y tons" where y is the maximum possible load
  • a blank line

Sample Input

4 3
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Muenchen
5 5
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Hamburg 220
Hamburg Muenchen 170
Muenchen Karlsruhe
0 0

Sample Output

Scenario #1
80 tons
 
Scenario #2
170 tons

Source

 
 
思路:给出N个地点,然后给出这些地点之间的路径可以承受的汽车最大的压力。首先将这N个地点映射为数字。然后建立矩阵。最后可以用求最短路径的方法进行变形,从而求出答案。
 
 
  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <string>
  7 #include <map>
  8 
  9 #define MAXINT 99999999
 10 
 11 using namespace std;
 12 
 13 int data[204][204];
 14 int dis[204];
 15 int isVis[204];
 16 
 17 
 18 char maps[204][34];
 19 int length=0;
 20 
 21 
 22 int findv(char str[34])
 23 {
 24     int i;
 25     for(i=1;i<length;i++)
 26     {
 27                          if(strcmp(maps[i],str)==0)
 28                          return i;
 29     }
 30     
 31     
 32 
 33 
 34 
 35 strcpy(maps[length],str);
 36 length++;
 37 
 38 return length-1;
 39 }
 40 
 41 
 42 
 43 int minn(int x,int y)
 44 {if(x>y)
 45 return y;
 46 return x;
 47 }
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 int main(int argc, char *argv[])
 56 {
 57               
 58               int i,j,k;
 59               int n,m;
 60               int ncount=0;
 61               
 62               
 63               while(scanf("%d%d",&n,&m)!=EOF)
 64               {
 65                                              if((n==0)&&(m==0))
 66                                              break;
 67                                              
 68                                              length=1;
 69                                              
 70                                              getchar();
 71                                              
 72                                              
 73                                              
 74                                              
 75                                              
 76                                              
 77                                              ncount++;
 78                                              
 79                                              for(i=1;i<=n;i++)
 80                                              for(j=1;j<=n;j++)
 81                                              data[i][j]=0;
 82                                              
 83                                              
 84                                              
 85                                              for(i=1;i<=m;i++)
 86                                              {
 87                                                               char s1[34],s2[34];
 88                                                               int w;
 89                                                               
 90                                                               scanf("%s%s%d",s1,s2,&w);
 91                                                               
 92                                                               getchar();
 93                                                               
 94                                                               
 95                                                               int v1=findv(s1);
 96                                                               int v2=findv(s2);
 97                                                               
 98                                                               
 99                                                               data[v1][v2]=data[v2][v1]=w;
100                                              }
101                                              
102                                              
103                                              char s1[34],s2[34];
104                                              
105                                              scanf("%s%s",s1,s2);
106                                              getchar();
107                                              
108                                              
109                                              int startv=findv(s1);
110                                              int endv=findv(s2);
111                                              
112                                              
113                                              
114                                              
115                                              
116                                              for(i=1;i<=n;i++)
117                                              isVis[i]=0;
118                                              
119                                              for(i=1;i<=n;i++)
120                                              {dis[i]=data[startv][i];}
121                                              
122                                              
123                                              
124                                              int maxdis=0;
125                                              isVis[startv]=1;
126                                              
127                                              for(i=1;i<=n;i++)
128                                              {
129                                                               k=-1;
130                                                               maxdis=0;
131                                                               
132                                                               for(j=1;j<=n;j++)
133                                                               {
134                                                                                if((isVis[j]==0)&&(maxdis<dis[j]))
135                                                                                {k=j;maxdis=dis[j];}
136                                                               }
137                                                               
138                                                               
139                                                               isVis[k]=1;
140                                                               
141                                                              
142                                                               for(j=1;j<=n;j++)
143                                                               {
144                                                                                int tmp=minn(dis[k],data[k][j]);
145                                                                                if((isVis[j]==0)&&(dis[j]<tmp))
146                                                                                {dis[j]=tmp;}
147                                                               }
148                                              }
149                                              
150                                              
151                                              printf("Scenario #%d\n",ncount);
152                                              
153 
154                                              
155                                              printf("%d tons\n\n",dis[endv]);
156 
157               }
158                                              
159                                              
160                                              
161                                                               
162                                                               
163                                              
164                                              
165                                              
166                                              
167                                                               
168                                                               
169                                                               
170                                                               
171                                                                      
172                             
173                             
174                             
175                             
176                             
177     
178     
179     
180     
181     //system("PAUSE");
182     return EXIT_SUCCESS;
183 }

 

转载于:https://www.cnblogs.com/zjushuiping/archive/2012/08/18/2645484.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值