UVA 10099 The Touris Guide

一樣是 shortest-path 的變型. 但這題目不是找最短的路徑, 而是找路徑中最少的值是所有可能路徑中最大的. 要注意的是, 導遊自身也佔一個位置, 在計算時要減一.

uva10099.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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int INFT = 1 << 30;
const int MAXN = 101;
int road[MAXN][MAXN];
int station[MAXN];
bool visited[MAXN];

int djk_algo(int n, int from, int to) {
memset(station, 0, sizeof(station));
memset(visited, false, sizeof(visited));
station[from] = INFT;

for (int k = 0; k < n; ++k) {
int a = -1, maxv = 0;
for (int i = 1; i <= n; ++i) {
if (!visited[i] && station[i] > maxv) {
a = i;
maxv = station[i];
}
}

if (a == -1) break;
visited[a] = true;

for (int i = 1; i <= n; ++i) {
if (visited[i] || road[a][i] == INFT) continue;
int value = min(station[a], road[a][i]);
if (value > station[i]) {
station[i] = value;
}
}
}

return station[to];
}


int main() {

int N, R;
int c1, c2, p;
int from, to, amount;
int count = 0;

while (scanf("%d%d", &N, &R) != EOF && (N || R)) {
memset(road, 0, sizeof(road));
for (int i = 0; i < R; ++i) {
scanf("%d%d%d", &c1, &c2, &p);
road[c1][c2] = p - 1;
road[c2][c1] = p - 1;
}

scanf("%d%d%d", &from, &to, &amount);

int cost = djk_algo(N, from, to);
cost = (int)(ceil((double)amount/cost));

printf("Scenario #%d\n", ++count);
printf("Minimum Number of Trips = %d\n\n", cost);
}
return 0;
}