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 and P. 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 T representing 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 10Sample Output
Scenario #1Minimum Number of Trips = 5
给出一个图, 求一条s到e的路径,使得这条路中最小边的权值尽可能大
求最大生成树, 当加入某条边使得s与e连通,则该边就是路径上最短的边
注意导游也是人,每条路能通过的最大人数等于权值-1
#include <iostream> #include <fstream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N = 100; struct Node { int s, e; int val; }q[N*N]; int p[N]; int cmp(Node a, Node b) { return a.val > b.val; } int find(int x) { return x == p[x]? x:find(p[x]); } int main(void) { // freopen("1.txt", "r", stdin); int n, m, sum; int kase = 1; while(scanf("%d %d", &m, &n) != EOF) { if(n + m == 0) break; for(int i = 0;i <= m;i++) p[i] = i; printf("Scenario #%d\n", kase++); int x, y, len, cnt = 0; for(int i = 0;i < n;i++) { cin>>x>>y>>len; q[cnt++] = {x, y, len}; } cin>>x>>y>>sum; int _min; sort(q, q+cnt, cmp); for(int i = 0;i < cnt;i++) { int fa = find(q[i].s); int fb = find(q[i].e); if(fa != fb) { p[fa] = fb; _min = q[i].val; } if(find(x) == find(y)) break; } printf("Minimum Number of Trips = %g\n\n",ceil(1.0*sum/(_min-1))); } return 0; }