Slow Leak(floyd 加油站)

https://vjudge.net/problem/Kattis-slowleak

题意:

从1到n,路径长度。有多个点可以加油,每次加完油后只能走一定的路程。500个点。

解析:

先做一遍Floyd,然后把加油站抽出来再做一遍Floyd即可。

代码:

/*
 *  Author : Jk_Chen
 *    Date : 2020-02-06-14.51.14
 */
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define rep(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define per(i,a,b) for(int i=(int)(a);i>=(int)(b);i--)
#define mmm(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pill pair<int, int>
#define fi first
#define se second
#define debug(x) cerr<<#x<<" = "<<x<<'\n'
const LL mod=1e9+7;
const int maxn=503;
const int inf=0x3f3f3f3f;
LL rd(){ LL ans=0; char last=' ',ch=getchar();
    while(!(ch>='0' && ch<='9'))last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans; return ans;
}
#define rd rd()
/*_________________________________________________________begin*/

int n,m,k;
LL V;

vector<int>pos;
LL dis[maxn][maxn];
LL INF;
void floyd(){
    rep(K,1,n)
    rep(i,1,n){
        rep(j,1,n){
            dis[j][i]=dis[i][j]=min(dis[i][j],dis[i][K]+dis[K][j]);
        }
    }
    rep(i,0,pos.size()-1){
        rep(j,0,pos.size()-1){
            if(dis[pos[i]][pos[j]]>V){
                dis[pos[i]][pos[j]]=INF;
            }
        }
    }
    rep(K,0,pos.size()-1)
    rep(i,0,pos.size()-1){
        rep(j,0,pos.size()-1){
            dis[pos[j]][pos[i]]=dis[pos[i]][pos[j]]=min(dis[pos[i]][pos[j]],dis[pos[i]][pos[K]]+dis[pos[K]][pos[j]]);
        }
    }
}

int main(){
    mmm(dis,0x3f);
    INF=dis[0][0];

    n=rd,m=rd,k=rd,V=rd;
    rep(i,1,n)dis[i][i]=0;
    rep(i,1,k){
        int p=rd;
        pos.pb(p);
    }
    pos.pb(1);
    pos.pb(n);

    rep(i,1,m){
        int a=rd,b=rd;
        LL v=rd;
        dis[a][b]=dis[b][a]=v;
    }
    floyd();
    if(dis[1][n]>=INF){
        puts("stuck");
    }
    else{
        printf("%lld\n",dis[1][n]);
    }
}
Slow Leak(floyd 加油站)Slow Leak(floyd 加油站) jk_chen_acmer 发布了734 篇原创文章 · 获赞 316 · 访问量 17万+ 他的留言板 关注
上一篇:CF1293A - ConneR and the A.R.C. Markland-N 题解


下一篇:UVA679 - Dropping Balls 题解