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;
using ll = long long;
const int N = 100000;
map<int, vector<pair<int, int>>> g[N];
map<int, ll> dist[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b, c, w;
cin >> a >> b >> c >> w;
--a, --b;
g[a][c].emplace_back(b, w);
g[b][c].emplace_back(a, w);
}
priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq;
auto relax = [&pq](int v, int c, ll d) {
if (!dist[v].count(c) || dist[v][c] > d) {
dist[v][c] = d;
pq.push({d, v, c});
}
};
dist[0][0] = 0;
pq.push({0, 0, 0});
//we create c dummy vertices, v_0 is main one, v_c is for cases when we come to v with color c, and go from color c
while (!pq.empty()) {
auto [d, v, i] = pq.top();
pq.pop();
if (dist[v][i] < d) {
continue;
}
if (i == 0) {
for (auto [c, adj] : g[v]) {
ll sum = 0;
for (auto [to, w] : adj) {
sum += w;
}
for (auto [to, w] : adj) {
relax(to, 0, d + min((ll)w, sum - w));
relax(to, c, d);
}
}
} else {
ll sum = 0;
for (auto [to, w] : g[v][i]) {
sum += w;
}
for (auto [to, w] : g[v][i]) {
relax(to, 0, d + sum - w);
}
}
}
cout << (dist[n - 1].count(0) ? dist[n - 1][0] : -1);
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... |