P2966 [USACO09DEC]牛收费路径Cow Toll Paths

近来又开始复习Floyd,(Floyd大法好,我吹爆Floyd啊啊!!!)这么简短的算法去哪里找QAQ。

这道题很普通的一道Floyd,只需要把一种数据改成多组数据(写代码5分钟,debug一小时,我输入忘打了一个%d,自己废了好长时间才看出来QAQ)

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 260;
const int inf = 1e9;
int dis[maxn][maxn], ans[maxn][maxn], f[maxn];
int n, m, x, y, z, t;
struct node {
    int v, id;
    friend  bool operator<(node x, node y) {
        if(x.v == y.v)
            return x.id < y.id;
        return x.v < y.v;
    }
} e[maxn];
int main() {
    scanf("%d %d %d", &n, &m, &t);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &e[i].v);
        ans[i][i] = e[i].v;
        e[i].id = i;
    }

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++) {
            dis[i][j] = dis[j][i] = inf;
            if(i != j)
                ans[i][j] = ans[j][i] = inf;
        }
    for(int i = 1; i <= n; i++)
        dis[i][i] = 0;
    sort(e + 1,e + n + 1);
    for(int i = 1; i <= n; i++)
        f[e[i].id] = i;

    for(int i = 1; i <= m; i++) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        if(w < dis[f[u]][f[v]])
            dis[f[u]][f[v]] = dis[f[v]][f[u]] = w;
    }
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++) {
                dis[j][i] = dis[i][j] = min(dis[i][k] + dis[k][j],dis[i][j]);
                ans[j][i] = ans[i][j] = min(ans[i][j],dis[i][j] + max(e[i].v,max(e[j].v,e[k].v)));
            }
    while(t--) {
        int a, b;
        scanf("%d %d", &a, &b);
        printf("%d\n", ans[f[a]][f[b]]);
    }
    return 0;
}

 

posted @ 2019-04-13 20:29  机器闵  阅读(93)  评论(0编辑  收藏  举报