This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 2e5 + 7;
const int MOD = 1e9 + 7; // 998244353;
const int INF = 1e9 + 7;
const long long INFLL = 1e18 + 7;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
struct edge {
int to, color;
long long cost;
edge() {}
edge(int _to, int _color, long long _cost) {
to = _to;
color = _color;
cost = _cost;
}
};
int n, m;
long long dist[N];
long long sum[N];
long long best[N];
vector<edge> adj[N];
void solve(void) {
cin >> n >> m;
for (int i = 1; i <= m; i ++) {
int u, v, c, w;
cin >> u >> v >> c >> w;
adj[u].emplace_back(v, c, w);
adj[v].emplace_back(u, c, w);
}
memset(dist, 0x3f, sizeof dist);
memset(best, 0x3f, sizeof best);
priority_queue<pair<long long, int>> heap;
dist[1] = 0;
heap.emplace(0, 1);
while (!heap.empty()) {
int u; long long d_u;
tie(d_u, u) = heap.top();
heap.pop();
d_u = -d_u;
if (d_u != dist[u]) continue;
for (edge x: adj[u]) {
sum[x.color] += x.cost;
minimize(best[x.color], dist[x.to]);
}
for (edge x: adj[u]) {
if (minimize(dist[x.to], d_u + min(1LL * x.cost, sum[x.color] - x.cost))) {
heap.emplace(-dist[x.to], x.to);
}
if (minimize(dist[x.to], best[x.color] + sum[x.color] - x.cost)) {
heap.emplace(-dist[x.to], x.to);
}
}
for (edge x: adj[u]) {
sum[x.color] = 0;
best[x.color] = INFLL;
}
}
cout << (dist[n] < INFLL ? dist[n] : -1);
}
int main(void) {
cin.tie(0)->sync_with_stdio(0);
int test = 1;
// cin >> test;
while (test --) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |