이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int int64_t
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ar = array<int, 2>;
using ar3 = array<int, 3>;
const int64_t INF = 1ll << 60;
const int N = 4e5 + 5;
vector<ar3> adj[N];
int n, m, C[N], d[N], sum[N], P[N];
bool cnt[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
FOR(i, 0, m) {
int u, v, c, p;
cin >> u >> v >> c >> p;
--u, --v;
adj[v].push_back({u, c, p});
adj[u].push_back({v, c, p});
}
int ptr = n - 1;
vector<array<int, 4>> E;
FOR(i, 0, n) {
for (auto [u, c, p]: adj[i]) {
P[c] = (cnt[c] ? P[c] : ++ptr);
cnt[c] = true;
sum[c] += p;
}
/*
for (auto &[u, c, p]: adj[i]) for (auto &[v, cv, pv]: adj[i]) if (cv == c && (u != v)) {
E.push_back({u, v, c, sum[c] - pv});
}
*/
for (auto [u, c, p]: adj[i]) {
E.push_back({u, P[c], c, 0});
E.push_back({P[c], u, c, sum[c] - p});
}
for (auto &[u, c, p]: adj[i]) {
p = min(p, sum[c] - p);
cnt[c] = 0;
}
for (auto [u, c, p]: adj[i]) sum[c] = 0;
}
for (auto [u, v, c, p]: E)
adj[u].push_back({v, c, p});
fill(d + 1, d + N, INF);
priority_queue<ar> q;
q.push({0, 0});
while (q.size()) {
auto [dist, v] = q.top();
q.pop();
if (dist > d[v]) continue;
for (auto [u, c, p]: adj[v]) {
int w = d[v] + p;
if (w < d[u]) {
q.push({-w, u});
d[u] = w;
}
}
}
cout << (d[n - 1] < INF ? d[n - 1] : -1) << '\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... |