結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | m_tsubasa |
提出日時 | 2020-11-27 22:35:28 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 270 ms / 3,000 ms |
コード長 | 2,259 bytes |
コンパイル時間 | 2,556 ms |
コンパイル使用メモリ | 219,096 KB |
実行使用メモリ | 42,788 KB |
最終ジャッジ日時 | 2024-07-26 19:55:34 |
合計ジャッジ時間 | 12,254 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 228 ms
41,560 KB |
testcase_03 | AC | 202 ms
37,204 KB |
testcase_04 | AC | 270 ms
38,784 KB |
testcase_05 | AC | 219 ms
41,312 KB |
testcase_06 | AC | 253 ms
36,360 KB |
testcase_07 | AC | 240 ms
38,680 KB |
testcase_08 | AC | 194 ms
37,600 KB |
testcase_09 | AC | 235 ms
34,304 KB |
testcase_10 | AC | 199 ms
37,180 KB |
testcase_11 | AC | 250 ms
37,628 KB |
testcase_12 | AC | 257 ms
37,476 KB |
testcase_13 | AC | 241 ms
41,776 KB |
testcase_14 | AC | 228 ms
34,740 KB |
testcase_15 | AC | 227 ms
36,184 KB |
testcase_16 | AC | 270 ms
38,780 KB |
testcase_17 | AC | 253 ms
41,880 KB |
testcase_18 | AC | 224 ms
38,084 KB |
testcase_19 | AC | 251 ms
36,352 KB |
testcase_20 | AC | 245 ms
35,072 KB |
testcase_21 | AC | 248 ms
39,988 KB |
testcase_22 | AC | 257 ms
35,968 KB |
testcase_23 | AC | 244 ms
41,400 KB |
testcase_24 | AC | 247 ms
35,840 KB |
testcase_25 | AC | 263 ms
38,912 KB |
testcase_26 | AC | 242 ms
37,828 KB |
testcase_27 | AC | 246 ms
37,980 KB |
testcase_28 | AC | 208 ms
41,156 KB |
testcase_29 | AC | 262 ms
38,000 KB |
testcase_30 | AC | 261 ms
39,020 KB |
testcase_31 | AC | 255 ms
38,400 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 154 ms
31,872 KB |
testcase_34 | AC | 252 ms
42,788 KB |
ソースコード
#include <bits/stdc++.h> #define inf (long long)(1e17) using namespace std; template <class T> struct Primal_Dual { using Pa = pair<T, int>; long long infinity = inf; 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; Primal_Dual<long long> pd; int main() { cin >> n >> m; pd = Primal_Dual<long long>(n); for (int i = 0; i < m; ++i) { int x, y, c, d; cin >> x >> y >> c >> d; pd.add(--x, --y, 1, c); pd.add(y, x, 1, c); pd.add(x, y, 1, d); pd.add(y, x, 1, d); } cout << pd.solve(0, n - 1, 2) << endl; return 0; }