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

题面:https://www.luogu.org/problem/P2966

本题直接在floyd的基础上加上最大点权即可.(先排序,保证点权最小)
Code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<set>
using namespace std;
const int N=255;
int n,m,K,dis[N][N],f[N][N],q[N];
struct Node{
    int id,c;
}g[N];
bool cmp(Node p,Node q){
    if(p.c==q.c){
        return p.id<q.id;
    }
    return p.c<q.c;
}
int main(){
    scanf("%d%d%d",&n,&m,&K);
    for(int i=1;i<=n;i++){
        g[i].id=i;
        scanf("%d",&g[i].c);
    }
    sort(g+1,g+1+n,cmp);
    for(int i=1;i<=n;i++){
        q[g[i].id]=i;
    }
    memset(f,0x3f,sizeof(f));
    memset(dis,0x3f,sizeof(dis));
    for(int i=1;i<=n;i++){
        f[i][i]=0;
    }
    for(int i=1;i<=m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        x=q[x];
        y=q[y];
        f[x][y]=min(f[x][y],z);
        f[y][x]=f[x][y];
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
                dis[i][j]=min(dis[i][j],f[i][j]+max(g[k].c,max(g[i].c,g[j].c)));
            }
        }
    }
    for(int i=1;i<=K;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        printf("%d\n",dis[q[x]][q[y]]);
    }
    return 0;
}
上一篇:DP五十题


下一篇:Luogu_P2886 [USACO07NOV]牛继电器Cow Relays【题解】图论 矩阵快速幂