UVa 10048 噪音恐惧症(Floyd)

时间:2021-02-17 03:02:47

传送门

题意

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

思路

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

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

其次,也可以用Dijkstra算法来计算。

代码

 #include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std; #define INF 1000001 int n, m, t;
int s, e; int map[][]; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int kase = ;
int a, b, w;
while (scanf("%d%d%d", &n, &m,&t))
{
if (m == && n == && t == ) break;
if (kase > ) printf("\n");
printf("Case #%d\n", kase++); for (int i = ; i <= n; i++)
{
map[i][i] = INF;
for (int j = i + ; j <= n; j++)
map[i][j] = map[j][i] = INF;
} for (int i = ; i < m; i++)
{
scanf("%d%d%d", &a, &b, &w);
map[a][b] = map[b][a] = w;
} for (int k = ; k <= n;k++)
for (int i = ; i <= n;i++)
for (int j = ; j <= n; j++)
map[i][j] = min(map[i][j], max(map[i][k], map[k][j])); for (int i = ; i < t; i++)
{
scanf("%d%d", &s, &e);
if (map[s][e] == INF) printf("no path\n");
else printf("%d\n", map[s][e]);
}
}
return ;
}
 #include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> pii; int c, s, q;
vector<pii> g[];
bool vis[];
int d[][]; struct HeapNode{
int d, u;
bool operator < (const HeapNode& rhs) const{
return d > rhs.d;
}
}; void dijkstra(int s){
memset(vis, , sizeof(vis));
for(int i=; i<=c; i++) d[s][i] = inf;
d[s][s] = ;
priority_queue<HeapNode> Q;
Q.push((HeapNode){, s});
while(!Q.empty()){
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if(vis[u]) continue;
vis[u] = true;
for(int i=; i<g[u].size(); i++){
int v = g[u][i].first;
if(d[s][v] > max(d[s][u], g[u][i].second)){
d[s][v] = max(d[s][u], g[u][i].second);
Q.push((HeapNode){d[s][v], v});
}
}
}
} int main(){
//freopen("in.txt", "r", stdin);
int cas = ;
while(~scanf("%d%d%d", &c, &s, &q) && c && s && q){
for(int i=; i<=c; i++) g[i].clear();
if(cas > ) puts("");
printf("Case #%d\n", cas++);
for(int i=; i<s; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
for(int i=; i<=c; i++){
dijkstra(i);
} while(q--){
int st, ed;
scanf("%d%d", &st, &ed);
if(d[st][ed] == inf) puts("no path");
else printf("%d\n", d[st][ed]);
}
}
return ;
}