Floyd算法解决POJ 2263
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.
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.
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
这道题目的关键就在于松驰操作的改变了。
View Code
#include "iostream"
using namespace std;
#define size 201
int map[size][size];
char name[size][500];
int n, begin, end, sum;
int Min(int a, int b)
{
return a>b?b:a;
}
int Str_Int(char name1[])
{
int i;
for(i=0; i<sum; i++)
if(strcmp(name1, name[i])==0)
return i+1;
strcpy(name[sum++], name1);
return sum;
}
void floyd()
{
for(int m=1; m<=n; m++)
{
for(int s=1; s<=n; s++)
{
if(map[s][m])
{
for(int e=1; e<=n; e++)
{
if(map[m][e])
{
if(map[s][e] < Min(map[s][m], map[m][e]))
map[s][e] = Min(map[s][m], map[m][e]);
}
}
}
}
}
}
int main()
{
int r, i, node1, node2, val;
char name1[200], name2[200];
int k=0;
while(cin>>n>>r && (n+r))
{
k++;
sum = 0;
memset(name, 0, sizeof(name));
memset(map, 0, sizeof(map));
for(i=0; i<r; i++)
{
cin>>name1>>name2>>val;
node1 = Str_Int(name1);
node2 = Str_Int(name2);
map[node1][node2]=map[node2][node1]=val;
}
floyd();
cin>>name1>>name2;
begin = Str_Int(name1);
end = Str_Int(name2);
cout<<"Scenario #"<<k<<endl<<map[begin][end]<<" tons"<<endl<<endl;
}
return 0;
}
posted on 2011-10-04 11:52 More study needed. 阅读(383) 评论(0) 编辑 收藏 举报