結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | jupiro |
提出日時 | 2020-12-02 17:44:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 203 ms / 3,000 ms |
コード長 | 3,249 bytes |
コンパイル時間 | 1,668 ms |
コンパイル使用メモリ | 146,976 KB |
実行使用メモリ | 43,268 KB |
最終ジャッジ日時 | 2024-09-13 10:35:37 |
合計ジャッジ時間 | 10,886 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 159 ms
41,052 KB |
testcase_03 | AC | 128 ms
36,736 KB |
testcase_04 | AC | 200 ms
38,784 KB |
testcase_05 | AC | 135 ms
40,960 KB |
testcase_06 | AC | 177 ms
36,224 KB |
testcase_07 | AC | 162 ms
38,144 KB |
testcase_08 | AC | 133 ms
37,248 KB |
testcase_09 | AC | 165 ms
34,304 KB |
testcase_10 | AC | 134 ms
36,608 KB |
testcase_11 | AC | 180 ms
37,376 KB |
testcase_12 | AC | 183 ms
37,248 KB |
testcase_13 | AC | 160 ms
41,268 KB |
testcase_14 | AC | 159 ms
34,432 KB |
testcase_15 | AC | 156 ms
35,840 KB |
testcase_16 | AC | 193 ms
38,656 KB |
testcase_17 | AC | 168 ms
41,376 KB |
testcase_18 | AC | 149 ms
37,632 KB |
testcase_19 | AC | 172 ms
36,224 KB |
testcase_20 | AC | 178 ms
34,944 KB |
testcase_21 | AC | 168 ms
39,552 KB |
testcase_22 | AC | 190 ms
35,840 KB |
testcase_23 | AC | 165 ms
40,960 KB |
testcase_24 | AC | 182 ms
35,712 KB |
testcase_25 | AC | 192 ms
38,784 KB |
testcase_26 | AC | 167 ms
37,504 KB |
testcase_27 | AC | 180 ms
37,632 KB |
testcase_28 | AC | 136 ms
40,704 KB |
testcase_29 | AC | 203 ms
37,760 KB |
testcase_30 | AC | 191 ms
38,656 KB |
testcase_31 | AC | 194 ms
38,272 KB |
testcase_32 | AC | 2 ms
6,944 KB |
testcase_33 | AC | 88 ms
31,872 KB |
testcase_34 | AC | 191 ms
43,268 KB |
ソースコード
#include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> #include <numeric> #include <cctype> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <utility> #include <fstream> using namespace std; typedef long long ll; using ull = unsigned long long; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } //template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; //constexpr long long mod = 1000000007LL; constexpr long long mod = 998244353; struct PrimalDual { struct edge { int to; ll cap; ll cost; int rev; bool isrev; edge(int _to, ll _cap, ll _cost, int _rev, bool _isrev) :to(_to), cap(_cap), cost(_cost), rev(_rev), isrev(_isrev) {} }; vector<vector<edge>> graph; vector<ll> potential, min_cost; vector<int> prevv, preve; PrimalDual(int V) :graph(V) {} void add_edge(int from, int to, ll cap, ll cost) { graph[from].emplace_back(to, cap, cost, graph[to].size(), false); graph[to].emplace_back(from, 0, -cost, graph[from].size() - 1, true); } ll min_cost_flow(int s, int t, ll f) { int V = graph.size(); ll res = 0; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq; potential.assign(V, 0); preve.assign(V, -1); prevv.assign(V, -1); while (f > 0) { min_cost.assign(V, INF); pq.emplace(0, s); min_cost[s] = 0; while (!pq.empty()) { auto cur = pq.top(); pq.pop(); if (min_cost[cur.second] < cur.first) continue; for (int i = 0; i < graph[cur.second].size(); i++) { edge& e = graph[cur.second][i]; ll nextCost = min_cost[cur.second] + e.cost + potential[cur.second] - potential[e.to]; if (e.cap > 0 and min_cost[e.to] > nextCost) { min_cost[e.to] = nextCost; prevv[e.to] = cur.second; preve[e.to] = i; pq.emplace(min_cost[e.to], e.to); } } } if (min_cost[t] == INF) return -1; for (int v = 0; v < V; v++) potential[v] += min_cost[v]; ll add_flow = f; for (int v = t; v != s; v = prevv[v]) { chmin(add_flow, graph[prevv[v]][preve[v]].cap); } f -= add_flow; res += add_flow * potential[t]; for (int v = t; v != s; v = prevv[v]) { edge& e = graph[prevv[v]][preve[v]]; e.cap -= add_flow; graph[v][e.rev].cap += add_flow; } } return res; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; PrimalDual g(n); for (int i = 0; i < m; i++) { int u, v, c, d; cin >> u >> v >> c >> d; u--; v--; g.add_edge(u, v, 1, c); g.add_edge(u, v, 1, d); g.add_edge(v, u, 1, c); g.add_edge(v, u, 1, d); } cout << g.min_cost_flow(0, n - 1, 2) << endl; }