이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge {
int to;
int c;
int 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 == 1) {
if (dp2[u][c] != -cost) {
continue;
}
for (auto v : g[u][c]) {
// we cant flip
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) {
// we dont flip j
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});
}
// we flip j but not another edge of the same colour
relax = edg.to - cost;
if (relax < dp[edg.to]) {
dp[edg.to] = relax;
pq.push({-dp[edg.to], edg.to, 0});
}
// we flip j and another edge of the same colour
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... |