UVa 10048 噪音恐惧症(Floyd)

https://vjudge.net/problem/UVA-10048

题意:

输入一个C个点S条边的无向带权图,边权表示该路径上的噪声值。输入一些询问,每次询问两个点,输出这两点间最大噪声值最小的路径。

 

思路:

最简单的方法就是Floyd算法。本来是求长度的,现在求最大噪声值最小的路径,稍微改一下就好了。

d[i][j]=min(d[i][j],max(d[i][k],d[k][j]))

代码如下:

 1 #include 
 2 #include
 3 #include<string>
 4 #include
 5 #include
 6 using namespace std;
 7 
 8 #define INF 1000001
 9 
10 int n, m, t;
11 int s, e;
12 
13 int map[105][105];
14 
15 
16 int main()
17 {
18     //freopen("D:\\txt.txt", "r", stdin);
19     int kase = 1;
20     int a, b, w;
21     while (scanf("%d%d%d", &n, &m,&t))
22     {
23         if (m == 0 && n == 0 && t == 0)  break;
24         if (kase > 1)   printf("\n");
25         printf("Case #%d\n", kase++);
26 
27         for (int i = 1; i <= n; i++)
28         {
29             map[i][i] = INF;
30             for (int j = i + 1; j <= n; j++)
31                 map[i][j] = map[j][i] = INF;
32         }
33 
34         
35         for (int i = 0; i < m; i++)
36         {
37             scanf("%d%d%d", &a, &b, &w);
38             map[a][b] = map[b][a] = w;
39         }
40 
41         for (int k = 1; k <= n;k++)
42         for (int i = 1; i <= n;i++)
43         for (int j = 1; j <= n; j++)
44             map[i][j] = min(map[i][j], max(map[i][k], map[k][j]));
45 
46         for (int i = 0; i < t; i++)
47         {
48             scanf("%d%d", &s, &e);
49             if (map[s][e] == INF)   printf("no path\n");
50             else  printf("%d\n", map[s][e]);
51         }
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/zyb993963526/p/6389117.html

你可能感兴趣的:(UVa 10048 噪音恐惧症(Floyd))