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>
using namespace std;
typedef long long ll;
struct Edge {
int to;
int c;
ll p;
};
const int N = 1e5 + 7;
const ll INF = 1e18;
int n, m;
map<int, vector<Edge>> g[N];
ll dp[N];
map<int, ll> dp2[N], psum[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b, c, p;
cin >> a >> b >> c >> p;
g[a][c].push_back({b, c, p});
g[b][c].push_back({a, c, p});
psum[a][c] += p;
psum[b][c] += p;
}
for (int i = 1; i <= n; ++i) {
dp[i] = INF;
}
priority_queue<tuple<ll, int, int>> pq;
dp[1] = 0;
pq.push({0, 1, 0});
while (!pq.empty()) {
ll cost;
int u, c;
tie(cost, u, c) = pq.top();
pq.pop();
if (c) {
if (dp2[u][c] != -cost) {
continue;
}
for (auto v : g[u][c]) {
ll relax = psum[u][c] - v.p;
if (relax - cost < dp[v.to]) {
dp[v.to] = relax - cost;
pq.push({-dp[v.to], v.to, 0});
}
}
}
else {
if (dp[u] != -cost) {
continue;
}
for (auto v : g[u]) {
for (auto edg : v.second) {
ll relax = psum[u][edg.c] - edg.p - cost;
if (relax < dp[edg.to]) {
dp[edg.to] = relax;
pq.push({-dp[edg.to], edg.to, 0});
}
relax = edg.p - cost;
if (relax < dp[edg.to]) {
dp[edg.to] = relax;
pq.push({-dp[edg.to], edg.to, 0});
}
relax = -cost;
if (!dp2[edg.to].count(edg.c) || relax < dp2[edg.to][edg.c]) {
dp2[edg.to][edg.c] = relax;
pq.push({-dp2[edg.to][edg.c], edg.to, edg.c});
}
}
}
}
}
if (dp[n] == INF) {
dp[n] = -1;
}
cout << dp[n];
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... |