UVa 10048 - Audiophobia

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

給你道路,請求出某點到某點,需要忍受噪音最小的路徑中的最大分貝值。並不是總和,而是點到點之間某一條路徑上的某一段路。
例如 A 到 B 有兩種走法:

  1. 50 + 80 + 30
  2. 50 + 90 + 1

我們要選擇的則為第一條,因為它所要忍受的最大分貝為 80 ,比第二條的 90 小,答案輸出 80 。

Solution

直接用 Floyd-Warshall 求出所有點到點之間所需的最大分貝即可。
不是計算總合, d[][] 的意義改為噪音最小的路徑中最大的分貝,所以稍微改一下迴圈內:

max = MAX(d[i][k], d[k][j])
if (max < d[i][j])
    d[j][i] = d[i][j] = max;

Code

UVa 10048UVa 10048 - Audiophobia
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
#include<cstdio>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define N 101

inline int input();
int main()
{
int C, S, Q, Case = 0;
int d[N][N];

while (C = input())
{
S = input();
Q = input();

int i, j, k;
//init
for (i = 0; i <= C; i++)
for (j = 0; j <= C; j++)
d[i][j] = 1e9;

for (i = 0; i < S; i++)
{
int c1, c2, decibel;
c1 = input(), c2 = input(), decibel = input();
d[c1][c2] = d[c2][c1] = decibel;
}

//Floyd-Warshall
for (k = 1; k <= C; k++)
for (i = 1; i <= C; i++)
for (j = 1; j <= C; j++)
{
int max = MAX(d[i][k], d[k][j]);//取最大的分貝
if (max < d[i][j])
d[j][i] = d[i][j] = max;
}

if (Case)
putchar('\n');
printf("Case #%d\n", ++Case);

for (int i = 0; i < Q; i++)
{
int c1, c2;
c1 = input(), c2 = input();

if (d[c1][c2] != 1e9)
printf("%d\n", d[c1][c2]);
else
puts("no path");
}
}

return 0;
}
int input()
{
int n = 0;
char c;
while ((c = getchar()) != ' '&&c != '\n')
n = n * 10 + c - '0';
return n;
}