Train(AtCoder - abc192_e)
code :
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;
typedef long long ll;
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;
}