洛谷P1266速度限制

传送门啦

看起来是一个最短路问题,但是引入了速度限制,就要写一下二维最短路了。

$ dis[i][j] $ :表示到i这个点,速度为j的最短时间。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 600;
const int maxm = 200050;

inline int read(){
    char ch = getchar();
    int f = 1 , x = 0;
    while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
    return x * f;
}

int n,m,d,a,b,v,l;
int head[maxn],tot;
double dis[maxn][maxn];
bool inq[maxn][maxn];

struct Edge {
    int from,to,val,next,len;
}edge[maxm << 1];

inline void add(int u,int v,int sp,int l){
    edge[++tot].from = u;
    edge[tot].to = v;
    edge[tot].val = sp;
    edge[tot].len = l;
    edge[tot].next = head[u];
    head[u] = tot;
}

struct Node {int u,v;};

Node pre[maxn][maxn];

inline void spfa(){
    queue<Node> q;
    q.push((Node) {0 , 70});
    for(int i=0;i<=n;i++)
        for(int j=0;j<=500;j++){
            dis[i][j] = 1e9;
            pre[i][j].u = pre[i][j].v = -1;
        }
    dis[0][70] = 0 , inq[0][70] = 1;
    while(!q.empty()){
        Node cur = q.front();
        q.pop();
        int u = cur.u , v = cur.v;
        inq[u][v] = 0;
        for(int i=head[u];i;i=edge[i].next){
            int to = edge[i].to;
            int vv = edge[i].val ? edge[i].val : v;
            if(dis[to][vv] > dis[u][v] + (double)edge[i].len / vv){
                dis[to][vv] = dis[u][v] + (double)edge[i].len / vv;
                pre[to][vv].u = u;
                pre[to][vv].v = v;
                if(!inq[to][vv]){
                    q.push((Node) {to , vv});
                    inq[to][vv] = 1;
                }
            }
        }
    }
}
inline void print(int u,int v){
    if(pre[u][v].u != -1) 
        print(pre[u][v].u , pre[u][v].v);
    printf("%d ",u);
}

int main(){
    n = read(); m = read(); d = read();
    for(int i=1;i<=m;i++){
        a = read(); b = read(); v = read(); l = read();
        add(a , b , v , l);
    }
    spfa();
    double minn = 1e9;
    int s = 0;
    for(int i=0;i<=500;i++)
        if(dis[d][i] < minn){
            minn = min(minn , dis[d][i]);
            s = i;
        }
    print(d , s);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Stephen-F/p/9911350.html