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. Sometwo-way roads connect the cities. For each pair of neighboring cities there isa bus service that runs only between those two cities and uses the road thatdirectly connects them. Each bus service has a limit on the maximum number ofpassengers it can carry. Mr. G. has a map showing the cities and the roadsconnecting them. He also has the information regarding each bus service. Heunderstands that it may not always be possible for him to take all the touriststo the destination city in a single trip. For example, consider the followingroad map of 7 cities. The edges connecting the cities represent the roads andthe number written on each edge indicates the passenger limit of the busservice that runs on that road.
![](http://uva.onlinejudge.org/external/100/p10099.gif)
Now, if he wantsto 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, andthe 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 ableto take all the tourists to the destination city in minimum number of trips.So, he seeks your help.
Input
The input will contain one ormore 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 ofroad segments. Then R lines willfollow each containing three integers: C1, C2and P. C1 and C2 are the city numbers and P (P> 1) is the limit on the maximumnumber 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 linewill contain three integers: S, D and T representing respectively the starting city, the destination cityand the number of tourists to be guided.
The input willend with two zeroes for N and R.
Output
For each test case in the inputfirst output the scenario number. Then output the minimum number of tripsrequired for this case on a separate line. Print a blank line after the output ofeach test case.
Sample Input
7 101 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 #1Minimum Number of Trips = 5
思路:按边的长度从大到小排序,每次合并两点后看s,d两点是否已经连通,如果已经连通,则该边就是所求边。
下面是代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAX=10100;
struct node
{
int u,v,t;
}edge[MAX];
int n,m;
int s,d,t;
int pre[110],rank[110];
bool cmp(node a,node b)
{
return a.t>b.t;
}
void init()
{
for(int i=0;i<=n;i++)
{
pre[i]=i;
rank[i]=i;
}
}
int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
}
void unite(int x,int y)
{
if(rank[x]<rank[y])
pre[x]=y;
else
{
pre[y]=x;
if(rank[x]==rank[y])
rank[x]++;
}
}
int Kruskal()
{
sort(edge,edge+m,cmp);
for(int i=0;i<m;i++)
{
int x=find(edge[i].u);
int y=find(edge[i].v);
if(x!=y)
{
unite(x,y);
if(find(s)==find(d))
return edge[i].t;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int x,y,f;
int loop=1;
while(cin>>n>>m,n+m)
{
init();
for(int i=0;i<m;i++)
cin>>edge[i].u>>edge[i].v>>edge[i].t;
cin>>s>>d>>t;
int k=Kruskal();
cout<<"Scenario #"<<loop++<<endl;
if(t%(k-1))
printf("Minimum Number of Trips = %d\n",t/(k-1)+1);//每次过去k-1个,因为导游要一起过去
else
printf("Minimum Number of Trips = %d\n",t/(k-1));
cout<<endl;
}
return 0;
}