1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <bits/stdc++.h>
#define debug(x) cerr << #x << " = " << x << endl #define int long long
using namespace std; typedef long long LL;
template <class T> void read(T& x) { x = 0; T f = 1; char ch = getchar(); while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar(); x *= f; }
template <class T, class... Args> void read(T& x, Args&... args) { read(x), read(args...); }
struct Edge { int to, k, t; };
const int MAXN = 1E5 + 5;
vector<Edge> G[MAXN];
int n, m, dis[MAXN], vis[MAXN], x, y;
void solve(int caseNum) { read(n, m, x, y); for (int i = 1; i <= m; i++) { int u, v, k, t; read(u, v, t, k); G[u].push_back(Edge{v, k, t}); G[v].push_back(Edge{u, k, t}); } for (int i = 1; i <= n; i++) dis[i] = 1e18; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; dis[x] = 0; q.push({0, x}); while (!q.empty()) { int now = q.top().second; q.pop(); if (vis[now]) continue; vis[now] = 1; for (auto& e : G[now]) { int to = e.to; int k = e.k; int t = e.t; int tmp = (dis[now] % k == 0 ? dis[now] / k : dis[now] / k + 1) * k + t; if (dis[to] > tmp) { dis[to] = tmp; q.push({dis[to], to}); } } } if (dis[y] == 1e18) cout << -1; else cout << dis[y]; }
signed main() { int testCase = 1; for (int i = 1; i <= testCase; i++) solve(i); return 0; }
|