Ultraviolet A 10099-The Tourist Guide (Floyd, maximum Spanning Tree)

Source: Internet
Author: User


Question:
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 shocould 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 in 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 andT 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 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


Analysis and Summary:
This is basically the same as that of the previous question, where, for all paths from a to B, there is a minimum weight value x, returns x of all paths.

After finding the x, to calculate a total of a few back and forth, each back and forth with a group of people to the target, then just need to directly T/(x-1) [up to get the whole], then get the answer, T is the total number of tour guides.

It is important to note that the number of people in each trip is X-1, because the tour guide also occupies a place.


Code:
1. Kruskal Method
[Cpp]
# Include <cstdio>
# Include <algorithm>
# Define N 10005
Using namespace std;
 
Int n, m, beg, end, limit, f [N], rank [N];
 
Struct Edge {
Int u, v, val;
Friend bool operator <(const Edge & a, const Edge & B ){
Return a. val> B. val;
}
} Arr [N];
 
Inline void init (){
For (int I = 0; I <= n; ++ I)
F [I] = I, rank [I] = 0;
}
Int find (int x ){
Int I, j = x;
While (j! = F [j]) j = f [j];
While (x! = J ){
I = f [x]; f [x] = j; x = I;
}
Return j;
}
Bool Union (int x, int y ){
Int a = find (x), B = find (y );
If (a = B) return false;
If (rank [a]> rank [B])
F [B] =;
Else {
If (rank [a] = rank [B])
++ Rank [B];
F [a] = B;
}
Return true;
}
 
 
Int main (){
Int a, B, c, cas = 1;
While (~ Scanf ("% d", & n, & m) & n + m ){
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & a, & B, & c );
Arr [I]. u = a, arr [I]. v = B, arr [I]. val = c;
}
Scanf ("% d", & beg, & end, & limit );
Init ();
Sort (arr, arr + m );
Int ans;
For (int I = 0; I <m; ++ I ){
Union (arr [I]. u, arr [I]. v );
A = find (beg), B = find (end );
If (a = B ){
Ans = arr [I]. val;
Break;
}
}
Int t = (limit % (ans-1) = 0 )? (Limit/(ans-1) :( limit/(ans-1) + 1 );
Printf ("Scenario # % d \ n", cas ++ );
Printf ("Minimum Number of Trips = % d \ n", t );
}
Return 0;
}

2. Floyd
[Cpp]
# Include <cstdio>
# Include <algorithm>
Const int n= 105;
Constint INF = 1000000000;
Using namespace std;
 
Int n, m, beg, end, limit, d [N] [N];
 
Inline void read_graph (){
For (int I = 1; I <= n; ++ I ){
D [I] [I] = 0;
For (int j = I + 1; j <= n; ++ j)
D [I] [j] = d [j] [I] = INF;
}
Int a, B, c;
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & a, & B, & c );
D [a] [B] = d [B] [a] = c;
}
}
 
Inline void Floyd (){
For (int k = 1; k <= n; ++ k)
For (int I = 1; I <= n; ++ I ){
For (int j = 1; j <= n; ++ j ){
Int tmp = min (d [I] [k], d [k] [j]);
If (d [I] [j] = INF) d [I] [j] = tmp;
Else d [I] [j] = max (d [I] [j], tmp );
}
}
}
 
Int main (){
Int a, B, c, cas = 1;
While (~ Scanf ("% d", & n, & m) & n + m ){
Read_graph ();
Scanf ("% d", & beg, & end, & limit );
Floyd ();
Int ans = d [beg] [end];
Int t = (limit % (ans-1) = 0 )? (Limit/(ans-1) :( limit/(ans-1) + 1 );
Printf ("Scenario # % d \ n", cas ++ );
Printf ("Minimum Number of Trips = % d \ n", t );
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.