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;
const int kN = 1e5;
unordered_map<int, vector<pair<int, int>>> g[1 + kN];
unordered_map<int, int64_t> sum[1 + kN], dp[1 + kN];
void testCase() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, c, w;
cin >> u >> v >> c >> w;
g[u][c].emplace_back(v, w);
g[v][c].emplace_back(u, w);
sum[u][c] += w;
sum[v][c] += w;
}
priority_queue<tuple<int64_t, int, int>, vector<tuple<int64_t, int, int>>, greater<tuple<int64_t, int, int>>> pq;
dp[1][0] = 0;
pq.emplace(0, 1, 0);
while (!pq.empty()) {
int64_t cost;
int u, c;
tie(cost, u, c) = pq.top();
pq.pop();
if (cost != dp[u][c]) {
continue;
}
if (c == 0) {
for (auto itr : g[u]) {
int c = itr.first;
for (auto it : itr.second) {
int v, w;
tie(v, w) = it;
if (!dp[v].count(c) || dp[v][c] > cost) {
dp[v][c] = cost;
pq.emplace(cost, v, c);
}
if (!dp[v].count(0) || dp[v][0] > cost + min((int64_t)w, sum[u][c] - w)) {
dp[v][0] = cost + min((int64_t)w, sum[u][c] - w);
pq.emplace(dp[v][0], v, 0);
}
}
}
} else {
for (auto it : g[u][c]) {
int v, w;
tie(v, w) = it;
if (!dp[v].count(0) || dp[v][0] > cost + sum[u][c] - w) {
dp[v][0] = cost + sum[u][c] - w;
pq.emplace(dp[v][0], v, 0);
}
}
}
}
if (!dp[n].count(0)) {
cout << "-1\n";
} else {
cout << dp[n][0] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
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... |