UVa10048 Audiophobia(floyd)

题意

给出一个图,图中的边表示从点u到点v路径上的噪音。给出q个查询,问从u到v所经路径上的最小噪音

思路

在使用floyd计算点对之间的路径时, D u , v k = m i n { D u , v k − 1 , m a x { D u , k k − 1 , D k , v k − 1 } } D_{u, v}^k= min \{D_{u, v}^{k - 1}, max\{D_{u, k}^{k- 1}, D_{k, v}^{k - 1}\}\} Du,vk=min{Du,vk1,max{Du,kk1,Dk,vk1}}
代码如下

#include <bits/stdc++.h>

using namespace std;

#define _for(i, a, b) for(int i = (a); i < (b); i++)
#define _rep(i, a, b) for (int i = (a); i <= (b); i++)

const int N = 104;
const int INF = 1e9;

int graph[N][N];

void fastio()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

int main()
{
    fastio();

    #ifndef ONLINE_JUDGE
        ifstream fin("f:\\OJ\\uva_in.txt");
        streambuf* back = cin.rdbuf(fin.rdbuf());
    #endif


    int kase = 1;
    int c, s, q;
    while (cin >> c >> s >> q) {
        if (c == 0 && s == 0 && q == 0) {
            break;
        }

        if (kase > 1) {
            cout << endl;
        }

        cout << "Case #" << kase++ << endl;
        _for(i, 0, c) {
            _for (j, 0, c) {
                graph[i][j] = (i == j) ? 0 : INF;
            }
        }

        _for(i, 0, s) {
            int c1, c2, d;
            cin >> c1 >> c2 >> d;

            --c1;
            --c2;
            graph[c1][c2] = min(graph[c1][c2], d);
            graph[c2][c1] = graph[c1][c2];
        }

        _for(k, 0, c) {
            _for(i, 0, c) {
                _for(j, 0, c) {
                    if (graph[i][k] != INF && graph[k][j] != INF) {
                        graph[i][j] = min(graph[i][j], max(graph[i][k], graph[k][j]));
                    }
                }
            }
        }

        _for(i, 0, q) {
            int c1, c2;
            cin >> c1 >> c2;
            --c1;
            --c2;
            int ans = graph[c1][c2];
            if (ans == INF) {
                cout << "no path" << endl;
            } else {
                cout << ans << endl;
            }
        }
    }

    #ifndef ONLINE_JUDGE
        cin.rdbuf(back);
    #endif

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值