이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define fore(x, v) for(auto &x : v)
#define fi first
#define se second
using namespace std;
const long long INF = 1e15;
const int N = 1e5+5;
const int M = 2e5+5;
int n, m;
map <int, vector <pair <int, int> > > graph[N];
long long dp[N];
map<int, long long> dp2[N], sum[N];
int main() {
cin >> n >> m;
foru(i, 1, m) {
int u, v, c, p;
cin >> u >> v >> c >> p;
graph[u][c].emplace_back(v, p);
graph[v][c].emplace_back(u, p);
sum[u][c] += p;
sum[v][c] += p;
}
memset(dp, 0x3f, sizeof(dp));
priority_queue <pair <long long, pair <int, int> > > pq;
dp[1] = 0;
pq.push({0, {1, 0}});
while (!pq.empty()) {
auto top = pq.top(); pq.pop();
long long cost = top.fi;
int u = top.se.fi, c = top.se.se;
if (!c) {
if (-cost != dp[u]) continue;
fore(nc, graph[u]) fore(info, nc.se) {
int v = info.fi, p = info.se;
long long ncost;
ncost = sum[u][nc.fi] - p - cost;
if (ncost < dp[v]) {
dp[v] = ncost;
pq.push({-ncost, {v, 0}});
}
ncost = p - cost;
if (ncost < dp[v]) {
dp[v] = ncost;
pq.push({-ncost, {v, 0}});
}
ncost = -cost;
if ((dp2[v].find(nc.fi) == dp2[v].end()) || ncost < dp2[v][nc.fi]) {
dp2[v][nc.fi] = ncost;
pq.push({-ncost, {v, nc.fi}});
}
}
}
else {
if (-cost != dp2[u][c]) continue;
long long ncost = sum[u][c] - cost;
fore(info, graph[u][c]) {
int v = info.fi, p = info.se;
if (ncost - p < dp[v]) {
dp[v] = ncost - p;
pq.push({-ncost + p, {v, 0}});
}
}
}
}
cout << (dp[n] < INF ? dp[n] : -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... |