Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1040
Topic:
Problem D
The tourist Guide
Input:standard input
Output:standard output
Mr G. Works as a tourist guide. His-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 are a bus service this runs only between those two cities and uses the road 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 service. He understands, it may, not always is possible for him to take "all" tourists to the "Destination City" a single tri P. 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 t The He bus service is runs on that road.
Now, if it wants to take tourists from City 1 to City 7, he'll 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 "he" able to take "all" tourists to th E destination city in minimum number of trips. So, he seeks your help.
Input
The input would contain one or more test cases. The ' a ' of each test case would contain two integers:n (n<=) and R representing respectively the number of C Ities and the number of road segments. Then R lines'll follow each containing three integers:c1, C2 ANDP. C1 and C2 are the city numbers and P (p> 1) are the limit on the maximum number of passengers to being carried by the bus s Ervice between the two cities. City numbers are positive integers ranging from 1 to N. The (R + 1)-th line would contain three integers:s, D Andt representing the respectively city, the starting CI Ty and the number of tourists to is guided.
The input would end with two zeroes for N and R.
Output
For each test case in the input, 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:
And the previous topic UVA 10048-audiophobia (Floyd, Kruskal) is basically similar to all the paths of A to B, each path has a minimum weight x, to find the largest x in all paths.
After the X, to calculate a total to walk a few round-trip, each back and forth with a group of people to the target, then only need direct t/(x-1) "Up", then get the answer, T for the overall number of guides.
Special attention is given to the number of x-1 per trip, as the guide also occupies a position.
Code:
1.Kruskal method
#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]=a;
else{if (rank[a]==rank[b]) ++rank[b];
F[a]=b;
return true;
int main () {int a,b,c,cas=1; while (~SCANF ("%d%d", &n,&m) &&n+m) {for (int i=0; i<m; ++i) {scanf ("%d%d%d",&
A,&B,&C); Arr[i].u=a,arR[i].v=b,arr[i].val=c;
} scanf ("%d%d%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 an 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\n", t);
return 0; }
2. Floyd