#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 100005
#define MAXM 200005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
#define f first
#define s second
ll n, m, d[MAXN], mn[MAXM], sm[MAXM];
vector<array<ll, 3>> e[MAXN];
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
bool v[MAXN];
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
ll u, v, w, c;
cin >> u >> v >> w >> c;
e[u].push_back({v, w, c});
e[v].push_back({u, w, c});
}
fill(d, d+n+1, 1e18);
d[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
ll D = pq.top().f;
ll s = pq.top().s;
pq.pop();
if (v[s]) continue;
v[s] = 1;
for (auto i : e[s]) mn[i[2]] = 1e18, sm[i[2]] = 0;
for (auto i : e[s]) sm[i[2]] += i[1];
for (auto i : e[s]) mn[i[2]] = min(mn[i[2]], d[i[0]] + sm[i[2]]);
for (auto i : e[s]) {
if (d[i[0]] > min({d[s] + i[1], d[s] + sm[i[2]] - i[1], mn[i[2]] - i[1]})) {
d[i[0]] = min({d[s] + i[1], d[s] + sm[i[2]] - i[1], mn[i[2]] - i[1]});
pq.push({d[i[0]], i[0]});
}
}
}
cout << (d[n] > 1e15 ? -1 : d[n]) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |