forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 10099 - The Tourist Guide.cpp
69 lines (61 loc) · 1.58 KB
/
UVa 10099 - The Tourist Guide.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Bus
{
int u, v, limit;
};
bool comp(const Bus &s1, const Bus &s2)
{
return s1.limit > s2.limit;
}
int getGroup(const vector<int> &groups, int u)
{
return groups[u] == u?
u : getGroup(groups, groups[u]);
}
int main()
{
int Case = 1, N, R;
while (cin >> N >> R, !(N == 0 && R == 0))
{
vector<Bus> buses;
vector<int> groups(N + 1);
for (int i = 0; i < R; ++i)
{
Bus bus;
cin >> bus.u >> bus.v >> bus.limit;
// Mr. G. needs a seat for himself.
--bus.limit;
buses.push_back(bus);
}
int S, D, T;
cin >> S >> D >> T;
sort(buses.begin(), buses.end(), comp);
for (int i = 1; i <= N; ++i)
groups[i] = i;
// Kruskal's algorithm.
for (int i = 0; i < R; ++i)
{
Bus &bus = buses[i];
int groupU = getGroup(groups, bus.u);
int groupV = getGroup(groups, bus.v);
if (groupU != groupV)
{
groups[groupV] = groupU;
if (getGroup(groups, S) == getGroup(groups, D))
{
cout << "Scenario #"
<< Case++
<< endl
<< "Minimum Number of Trips = "
<< (T + bus.limit - 1) / bus.limit
<< endl << endl;
break;
}
}
}
}
return 0;
}