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 <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
#define int long long
const int maxn = 1e5;
map<int, vector<pair<int, int>>> g[maxn];
map<int, int> dist[maxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
set<pair<pair<int, int>, int>> s;
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, c, p;
cin >> u >> v >> c >> p;
--u; --v;
g[u][c].push_back({ v, p });
g[v][c].push_back({ u, p });
}
dist[0][0] = 0;
s.insert({ { 0, 0 }, 0 });
auto upd = [&] (int u, int c, int d) {
if (dist[u].count(c)) {
if (dist[u][c] > d) {
dist[u][c] = min(dist[u][c], d);
s.insert({ { dist[u][c], u }, c });
}
} else {
dist[u][c] = d;
s.insert({ { dist[u][c], u }, c });
}
};
while (!s.empty()) {
int v = s.begin()->first.second;
int c = s.begin()->second;
int d = s.begin()->first.first;
s.erase(s.begin());
if (d > dist[v][c]) continue;
if (c == 0) {
for (auto [color, adjacent] : g[v]) {
int sum = 0;
for (auto [u, w] : adjacent) {
sum += w;
}
for (auto [u, w] : adjacent) {
upd(u, 0, d + min(w, sum - w));
upd(u, color, d);
}
}
} else {
int sum = 0;
for (auto [u, w] : g[v][c]) {
sum += w;
}
for (auto [u, w] : g[v][c]) {
upd(u, 0, d + sum - w);
}
}
}
cout << (dist[n - 1].count(0) ? dist[n - 1][0] : -1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |