10099 - The Tourist Guide//floyd

Problem D
The Tourist Guide
Input: standard input
Output: standard output

Mr. G. works as a tourist guide. His current assignment is to take some tourists from one city to another. Some two-way roads connect the cities. For each pair of neighboring cities there is a bus service that runs only between those two cities and uses the road that directly connects them. Each bus service has a limit on the maximum number of passengers it can carry. Mr. G. has a map showing the cities and the roads connecting them. He also has the information regarding each bus service. He understands that it may not always be possible for him to take all the tourists to the destination city in a single trip. For example, consider the following road map of 7 cities. The edges connecting the cities represent the roads and the number written on each edge indicates the passenger limit of the bus service that runs on that road.

Now, if he wants to take 99 tourists from city 1 to city 7, he will require at least 5 trips, since he has to ride the bus with each group, and the route he should take is : 1 - 2 - 4 - 7.
But, Mr. G. finds it difficult to find the best route all by himself so that he may be able to take all the tourists to the destination city in minimum number of trips. So, he seeks your help.

Input
The input will contain one or more test cases. The first line of each test case will contain two integers: N (N<= 100) and R representing respectively the number of cities and the number of road segments. Then R lines will follow each containing three integers: C1, C2 andP. C1 and C2 are the city numbers and P (P> 1) is the limit on the maximum number of passengers to be carried by the bus service between the two cities. City numbers are positive integers ranging from 1 to N. The (R + 1)-th line will contain three integers: S, D and Trepresenting respectively the starting city, the destination city and the number of tourists to be guided.
The input will end with two zeroes for N and R.

Output
For each test case in the input first output the scenario number. Then output the minimum number of trips required for this case on a separate line. Print a blank line after the output of each test case.

Sample Input
7 10
1 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 7 99
0 0

Sample Output
Scenario #1
Minimum Number of Trips = 5

【10099 - The Tourist Guide//floyd】Rezaul Alam Chowdhury
题目分析:这道题就是Floyd的变形。
代码:

#include #include #include #define Min(a,b) (a < b ? a : b) using namespace std; const int maxn = 110; const int MIN = -1; int G[maxn][maxn]; int d[maxn][maxn]; int N,R; void Floyd() { for(int k = 1; k <= N; k++) { for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { int temp = Min(d[i][k],d[k][j]); if(G[i][k] && G[k][j] && d[i][j] < temp) { d[i][j] = temp; } } } } } int main() { int c1,c2,p,T=0; while(scanf("%d%d",&N,&R) && N && R) { for(int i = 0 ; i < maxn; i++) { for(int j = 0; j < maxn; j++) { if(i == j) d[i][j] = 0; else d[i][j] = MIN; G[i][j] = 0; } } for(int i = 0; i < R; i++) { scanf("%d%d%d",&c1,&c2,&p); G[c1][c2] = G[c2][c1] = 1; d[c1][c2] = d[c2][c1] = p; } Floyd(); scanf("%d%d%d",&c1,&c2,&p); int ans = p/(d[c1][c2] - 1); if(p%(d[c1][c2]-1)) ans += 1; printf("Scenario #%d\n",++T); printf("Minimum Number of Trips = %d\n",ans); printf("\n"); } return 0; }




    推荐阅读