Train(AtCoder - abc192_e)

Train(AtCoder - abc192_e)

vj 链接:https://vjudge.ppsucxtt.cn/problem/AtCoder-abc192_e

原链接:https://atcoder.jp/contests/abc192/tasks/abc192_e?

code :

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;
typedef long long ll;

/*
无向图
点到点的最短路
双向边共为2e5 点为1e5
bfs优化 - 优先队列
*/

const int N = 3e5+10;
const ll mxx = 1e18;
int e[N], pr[N], h[N], idx = 0;
ll t[N], k[N], st[N];
bool vis[N];

void add(int a, int b, ll c, ll d){
    e[idx] = b, t[idx] = c, k[idx] = d, pr[idx] = h[a], h[a] = idx++;
}

struct node {
    int x; ll d;
    bool operator < (const node &b) const {
        return d > b. d;
    }
};

priority_queue<node>qq;
void bfs(int x, int y){
    st[x] = 0;
    qq.push({x,0});

    while(!qq.empty()){
        int p = qq.top().x;
        qq.pop();
        if(vis[p]) continue;
        vis[p] = 1;

        for(int i = h[p]; i != -1; i = pr[i]){
            ll tm = 0;
            int j = e[i];
            if(st[p] == 0) tm = 0;
            else tm = ceil(st[p]*1.0 / k[i]) *k[i];
            if(st[j] > tm + t[i]) st[j] = tm+t[i], qq.push({j, st[j]}); 
        }
    }

    if(st[y] == mxx) cout<< -1 <<endl;
    else cout<< st[y] <<endl;
}

int main(){
    int n, m, x, y;
    scanf("%d%d%d%d",&n, &m, &x, &y);
    
    memset(h, -1, sizeof h);
    for(int i = 1; i <= n; i++) st[i] = mxx;

    while(m--){
        int a, b, c, d;
        scanf("%d%d%d%d",&a,&b, &c, &d);
        add(a, b, c, d);
        add(b, a, c, d);
    }
    
    bfs(x, y);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值