pc110903 The Tourist Guide (dp)
|
![]() |
||||
![]() |
![]() |
![]() |
Mr. G. works as a tourist guide in Bangladesh. His current assignment is to show a group of tourists a distant city. As in all countries, certain pairs of cities are connected by two-way roads. Each pair of neighboring cities has a bus service that runs only between those two cities and uses the road that directly connects them. Each bus service has a particular limit on the maximum number of passengers it can carry. Mr. G. has a map showing the cities and the roads connecting them, as well as the service limit for each each bus service.
It is not always possible for him to take all the tourists to the destination city in a single trip. For example, consider the following road map of seven cities, where the edges represent roads and the number written on each edge indicates the passenger limit of the associated bus service.

It will take at least five trips for Mr. G. to take 99 tourists from city 1 to city 7, since he has to ride the bus with each group. The best route to take is 1 - 2 - 4 - 7.
Help Mr. G. find the route to take all his tourists to the destination city in the minimum number of trips.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers: N (N100) and R, representing the number of cities and the number of road segments, respectively. Each of the next R lines will contain three integers (C1, C2 and P) where C1 and C2 are the city numbers and P (P > 1) is the maximum number of passengers that can 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 T) representing, respectively, the starting city, the destination city, and the number of tourists to be guided.
The input will end with two zeros for N and R.
Output
For each test case in the input, first output the scenario number and then the minimum number of trips required for this case on a separate line. Print a blank line after the output for 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
分析:d[i][j]表示从第 i 个顶点到第 j 个顶点的最优值;故d[i][j]=max( min(d[i][k],d[k][j]) ) i<=k<=j;
心得:这个题还真纠结,起初题目意思没搞懂,案例都说不通,后来叫qjb看下,才明白导游每次都在车上,所以算出来的最优值还要减去一。
知道意思后,果断写了个爆搜,于是果断TLE了,后来看了别人的解题报告才知道是floyd,可是不知道这算法,看了下别人的解题报告,也就dp一个。

1 #include<iostream> 2 #include<cstdio> 3 #define N 110 4 5 using namespace std; 6 7 int d[N][N]; 8 9 int main() 10 { 11 int i,j,k,n,m,u,v,w,test=0,ans; 12 while(scanf("%d%d",&n,&m)&&(n||m)) 13 { 14 for(i=1;i<=n;i++) 15 { 16 for(j=1;j<=n;j++) 17 { 18 d[i][j]=(i==j?1000000000:0); 19 } 20 } 21 for(i=1;i<=m;i++) 22 { 23 scanf("%d%d%d",&u,&v,&w); 24 d[u][v]=d[v][u]=w; 25 } 26 scanf("%d%d%d",&u,&v,&w); 27 for(k=1;k<=n;k++) 28 { 29 for(i=1;i<=n;i++) 30 { 31 for(j=1;j<=n;j++) 32 { 33 if(d[i][j]<d[i][k]&&d[i][j]<d[k][j]) 34 { 35 d[i][j]=d[i][k]<d[k][j]?d[i][k]:d[k][j]; 36 } 37 } 38 } 39 } 40 ans=w/(d[u][v]-1); 41 if(w%(d[u][v]-1)!=0) ans++; 42 printf("Scenario #%d\nMinimum Number of Trips = %d\n\n",++test,ans); 43 } 44 return 0; 45 }