結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | m_tsubasa |
提出日時 | 2020-11-27 22:21:49 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,352 bytes |
コンパイル時間 | 2,812 ms |
コンパイル使用メモリ | 232,988 KB |
実行使用メモリ | 72,228 KB |
最終ジャッジ日時 | 2024-07-26 19:45:59 |
合計ジャッジ時間 | 17,679 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 2 ms
6,944 KB |
testcase_33 | AC | 218 ms
61,776 KB |
testcase_34 | AC | 535 ms
72,228 KB |
ソースコード
#include <bits/stdc++.h> #define inf (long long)(1e17) using namespace std; template <class T> struct Primal_Dual { using Pa = pair<T, int>; int infinity = (int)(1e9); struct edge { int to; T cap, cost; int rev; }; int v; vector<vector<edge>> edges; vector<T> h; vector<T> dist; vector<int> prevv, preve; Primal_Dual(int vsize = 1) { v = vsize; edges.resize(v); h.resize(v); dist.resize(v); prevv.resize(v); preve.resize(v); } bool add(int from, int to, T cap, T cost) { edges[from].push_back((edge){to, cap, cost, (int)edges[to].size()}); edges[to].push_back((edge){from, 0, -cost, (int)edges[from].size() - 1}); return 1; } T solve(int s, int t, T f) { T ans = 0; h.assign(v, 0); while (f > 0) { priority_queue<Pa, vector<Pa>, greater<Pa>> qu; dist.assign(v, infinity); dist[s] = 0; qu.push({0, s}); while (!qu.empty()) { Pa now = qu.top(); qu.pop(); int nowv = now.second; if (dist[nowv] < now.first) continue; for (int i = 0; i < (int)edges[nowv].size(); ++i) { edge &e = edges[nowv][i]; if (e.cap > 0 && dist[e.to] > dist[nowv] + e.cost + h[nowv] - h[e.to]) { dist[e.to] = dist[nowv] + e.cost + h[nowv] - h[e.to]; prevv[e.to] = nowv; preve[e.to] = i; qu.push({dist[e.to], e.to}); } } } if (dist[t] == infinity) return -1; for (int i = 0; i < v; ++i) h[i] += dist[i]; T d = f; for (int i = t; i != s; i = prevv[i]) d = min(d, edges[prevv[i]][preve[i]].cap); f -= d; ans += d * h[t]; for (int i = t; i != s; i = prevv[i]) { edge &e = edges[prevv[i]][preve[i]]; e.cap -= d; edges[i][e.rev].cap += d; } } return ans; } }; using P = pair<long long, long long>; int n, m; vector<map<int, long long>> g, g2; priority_queue<P, vector<P>, greater<P>> pq; Primal_Dual<long long> pd; long long solve(); int main() { cin >> n >> m; g.resize(n); g2.resize(n); pd = Primal_Dual<long long>(n); for (int i = 0; i < m; ++i) { int x, y, c, d; cin >> x >> y >> c >> d; --x, --y; pd.add(x, y, 1, c); pd.add(y, x, 1, c); pd.add(x, y, 1, d); pd.add(y, x, 1, d); g[x][y] = g[y][x] = c; g2[x][y] = g2[y][x] = d; } cout << solve() << endl; return 0; } long long solve() { return pd.solve(0, n - 1, 2); long long res = 0; auto dijk = [](vector<map<int, long long>> &g, vector<long long> &dist, vector<long long> &par) { dist.assign(n, inf); par.assign(n, -1); pq.push(P(0, 0)); dist[0] = 0; while (pq.size()) { auto [d, now] = pq.top(); pq.pop(); if (d != dist[now]) continue; for (auto [to, cost] : g[now]) if (d + cost < dist[to]) { dist[to] = d + cost; par[to] = now; pq.push(P(dist[to], to)); } } return dist[n - 1]; }; vector<long long> dist, par; res += dijk(g, dist, par); int now = n - 1; while (now != 0) { int to = par[now]; g[to][now] = g[now][to] = -1; now = to; } for (int i = 0; i < n; ++i) for (auto [to, cost] : g[i]) if (cost >= 0) g2[i][to] = cost; return res + dijk(g2, dist, par); }