当前位置: 代码迷 >> 综合 >> Uva - 10048 - Audiophobia(Floyd)
  详细解决方案

Uva - 10048 - Audiophobia(Floyd)

热度:7   发布时间:2024-01-10 13:21:35.0

题意:一个有C个点S条边的连通无向图,每条边有一非负权值(噪音),有Q组询问,每组询问从点c1到点c2(c1 != c2)最小需要忍受多大的噪音(非和)(C <= 100, S <= 1000, Q <= 10000)。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22156

——>>发现Floyd算法也可以处理这一种多源问题。

#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int maxn = 100 + 10;
const int INF = 0x3f3f3f3f;int d[maxn][maxn], C, S, Q, kase, flag;void init(){memset(d, 0x3f, sizeof(d));for(int i = 1; i <= C; i++) d[i][i] = 0;
}void read(){init();int u, v, w;for(int i = 0; i < S; i++){scanf("%d%d%d", &u, &v, &w);d[u][v] = d[v][u] = w;}
}void Floyd(){for(int k = 1; k <= C; k++)for(int i = 1; i <= C; i++)for(int j = 1; j <= C; j++)d[i][j] = min(d[i][j], max(d[i][k], d[k][j]));
}void solve(){if(flag) flag = 0;else puts("");printf("Case #%d\n", kase++);int u, v;for(int i = 0; i < Q; i++){scanf("%d%d", &u, &v);if(d[u][v] == INF) puts("no path");else printf("%d\n", d[u][v]);}
}int main()
{kase = 1;flag = 1;while(scanf("%d%d%d", &C, &S, &Q) == 3){if(!C && !S && !Q) return 0;read();Floyd();solve();}return 0;
}