UVaOJ 10048 - Audiophobia


——by A Code Rabbit

Description

给你一张图。

求图上任意两点间,经过的路径中最大值最小的路径。

输入以边的方式表达的图。

输出所求路径上的最大值。


Types

Graph Algorithms


Analysis

询问任意两点间的最优路径,可以想到Floyd。

只要修改Floyd选边的策略就可以了。


Solution

// UVaOJ 10048
// Audiophobia
// by A Code Rabbit

#include <algorithm>
#include <cstdio>
using namespace std;

const int MAXV = 102;
const int INF = 1e9;

template <typename T>
struct Graph {
    T mat[MAXV][MAXV];
    void Init(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                mat[i][j] = i == j ? 0 : INF;
            }
        }
    }
    void AddEdge(int u, int v, T w) {
        mat[u][v] = w;
    }
};

namespace Floyd {
    template <typename T>
    void Go(T w[MAXV][MAXV], int n) {
        for (int k = 0; k < n; k++)
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    if (max(w[i][k], w[k][j]) < w[i][j])
                        w[i][j] = max(w[i][k], w[k][j]);
    }
}

int c, s, q;
int c1, c2, d;
Graph<int> graph;

int main() {
    int tot_case = 0;
    while (scanf("%d%d%d", &c, &s, &q) && (c || s || q)) {
        // Input.
        graph.Init(c);
        for (int i = 0; i < s; i++) {
            scanf("%d%d%d", &c1, &c2, &d);
            graph.AddEdge(c1 - 1, c2 - 1, d);
            graph.AddEdge(c2 - 1, c1 - 1, d);
        }
        // Solve.
        Floyd::Go(graph.mat, c);
        // Output.
        printf("%s", tot_case ? "\n" : "");
        printf("Case #%d\n", ++tot_case);
        for (int i = 0; i < q; i++) {
            scanf("%d%d", &c1, &c2);
            if (graph.mat[c1 - 1][c2 - 1] != INF)
                printf("%d\n", graph.mat[c1 - 1][c2 - 1]);
            else
                printf("no path\n");
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值