結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー | scol_kp |
提出日時 | 2020-11-28 00:37:10 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 174 ms / 3,000 ms |
コード長 | 7,346 bytes |
コンパイル時間 | 2,556 ms |
コンパイル使用メモリ | 222,052 KB |
実行使用メモリ | 37,648 KB |
最終ジャッジ日時 | 2024-07-26 21:27:08 |
合計ジャッジ時間 | 8,860 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 140 ms
35,972 KB |
testcase_03 | AC | 114 ms
32,068 KB |
testcase_04 | AC | 168 ms
34,748 KB |
testcase_05 | AC | 113 ms
35,440 KB |
testcase_06 | AC | 150 ms
32,492 KB |
testcase_07 | AC | 140 ms
34,284 KB |
testcase_08 | AC | 112 ms
32,448 KB |
testcase_09 | AC | 134 ms
30,728 KB |
testcase_10 | AC | 117 ms
32,144 KB |
testcase_11 | AC | 148 ms
33,336 KB |
testcase_12 | AC | 151 ms
33,304 KB |
testcase_13 | AC | 140 ms
36,176 KB |
testcase_14 | AC | 131 ms
30,884 KB |
testcase_15 | AC | 128 ms
31,688 KB |
testcase_16 | AC | 168 ms
34,736 KB |
testcase_17 | AC | 146 ms
36,576 KB |
testcase_18 | AC | 129 ms
33,152 KB |
testcase_19 | AC | 150 ms
32,596 KB |
testcase_20 | AC | 155 ms
31,492 KB |
testcase_21 | AC | 143 ms
34,852 KB |
testcase_22 | AC | 156 ms
32,352 KB |
testcase_23 | AC | 146 ms
35,804 KB |
testcase_24 | AC | 158 ms
33,056 KB |
testcase_25 | AC | 172 ms
35,216 KB |
testcase_26 | AC | 142 ms
33,636 KB |
testcase_27 | AC | 142 ms
33,424 KB |
testcase_28 | AC | 124 ms
35,256 KB |
testcase_29 | AC | 174 ms
34,144 KB |
testcase_30 | AC | 159 ms
34,528 KB |
testcase_31 | AC | 165 ms
34,232 KB |
testcase_32 | AC | 2 ms
6,940 KB |
testcase_33 | AC | 91 ms
29,564 KB |
testcase_34 | AC | 159 ms
37,648 KB |
ソースコード
#include <bits/stdc++.h> #define FASTIO using namespace std; using ll = long long; using Vi = std::vector<int>; using Vl = std::vector<ll>; using Pii = std::pair<int, int>; using Pll = std::pair<ll, ll>; constexpr int I_INF = std::numeric_limits<int>::max(); constexpr ll L_INF = std::numeric_limits<ll>::max(); template <typename T1, typename T2> inline bool chmin(T1& a, const T2& b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> inline bool chmax(T1& a, const T2& b) { if (a < b) { a = b; return true; } return false; } template <ostream& os = std::cout> class Prints { private: class __Prints { public: __Prints(const char* sep, const char* term) : sep(sep), term(term) {} template <class... Args> auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); } template <typename T> auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) { for (size_t i = 0; i < sz; i++) os << vec[i] << (i == sz - 1 ? term : sep); } template <typename T> auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) { for (size_t i = 0; i < h; i++) for (size_t j = 0; j < w; j++) os << mat[i][j] << (j == w - 1 ? term : sep); } private: const char *sep, *term; void print() const { os << term; } void print_rest() const { os << term; } template <class T, class... Tail> void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); } template <class T, class... Tail> void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); } }; public: Prints() {} __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); } }; Prints<> prints; Prints<std::cerr> prints_err; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% template <typename Cap, typename Cost> class MinCostFlowGraph { private: struct _Edge { int to, rev; Cap cap; Cost cost; _Edge() noexcept {} _Edge(int to, Cap cap, Cost cost, int rev) noexcept : to(to), rev(rev), cap(cap), cost(cost) {} }; std::vector<std::vector<_Edge>> g; std::vector<std::pair<int, int>> pos; public: MinCostFlowGraph(int n) noexcept : g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) noexcept { const int m = static_cast<int>(pos.size()); const int from_id = static_cast<int>(g[from].size()); int to_id = static_cast<int>(g[to].size()); pos.emplace_back(from, static_cast<int>(g[from].size())); if (from == to) ++to_id; g[from].emplace_back(to, cap, cost, to_id); g[to].emplace_back(from, 0, -cost, from_id); return m; } struct Edge { int from, to; Cap cap, flow; Cost cost; Edge() noexcept {} Edge(int from, int to, Cap cap, Cap flow, Cost cost) noexcept : from(from), to(to), cap(cap), flow(flow), cost(cost) {} }; Edge get_edge(int i) const noexcept { const auto e = g[pos[i].first][pos[i].second]; const auto re = g[e.to][e.rev]; return Edge(pos[i].first, e.to, e.cap + re.cap, re.cap, e.cost); } std::vector<Edge> edges() const noexcept { const int m = static_cast<int>(pos.size()); std::vector<Edge> res; res.reserve(m); for (int i = 0; i < m; i++) res.emplace_back(get_edge(i)); return res; } std::pair<Cap, Cost> flow(int s, int t) noexcept { return flow(s, t, std::numeric_limits<Cap>::max()); } std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) noexcept { return slope(s, t, flow_limit).back(); } std::vector<std::pair<Cap, Cost>> slope(int s, int t) noexcept { return slope(s, t, std::numeric_limits<Cap>::max()); } std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) noexcept { constexpr Cost C_INF = std::numeric_limits<Cost>::max(); const int n = static_cast<int>(g.size()); std::vector<Cost> h(n), dist(n); std::vector<int> prevv(n), preve(n); std::vector<std::pair<Cap, Cost>> res; struct Q { Cost key; int to; Q(Cost key, int to) noexcept : key(key), to(to) {} bool operator>(const Q& r) const noexcept { return key > r.key; } }; Cap flow = 0; Cost cost = 0, prev_cost = -1; res.emplace_back(flow, cost); while (flow < flow_limit) { std::priority_queue<Q, std::vector<Q>, std::greater<Q>> que; dist.assign(n, C_INF); dist[s] = 0; que.emplace(0, s); while (!que.empty()) { auto [d, v] = que.top(); que.pop(); if (dist[v] < d) continue; const int m = static_cast<int>(g[v].size()); for (int i = 0; i < m; i++) { const _Edge& e = g[v][i]; Cost cn = dist[v] + e.cost + h[v] - h[e.to]; if (e.cap > 0 && dist[e.to] > cn) { dist[e.to] = cn; prevv[e.to] = v; preve[e.to] = i; que.emplace(cn, e.to); } } } if (dist[t] == C_INF) break; for (int i = 0; i < n; i++) h[i] += dist[i]; Cap f = flow_limit - flow; for (int i = t; i != s; i = prevv[i]) { f = std::min(f, g[prevv[i]][preve[i]].cap); } for (int i = t; i != s; i = prevv[i]) { _Edge& e = g[prevv[i]][preve[i]]; e.cap -= f; g[i][e.rev].cap += f; } Cost d = h[t]; flow += f; cost += f * d; if (prev_cost == d) res.pop_back(); res.emplace_back(flow, cost); prev_cost = d; } return res; } }; void solve() { int N, M; cin >> N >> M; MinCostFlowGraph<ll, ll> mcf(N); for (ll i = 0; i < M; i++) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b; mcf.add_edge(a, b, 1, c); mcf.add_edge(a, b, 1, d); mcf.add_edge(b, a, 1, c); mcf.add_edge(b, a, 1, d); } auto flw = mcf.flow(0, N - 1, 2); prints()(flw.second); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% int main() { #ifdef FASTIO std::cin.tie(nullptr), std::cout.tie(nullptr); std::ios::sync_with_stdio(false); #endif #ifdef FILEINPUT std::ifstream ifs("./in_out/input.txt"); std::cin.rdbuf(ifs.rdbuf()); #endif #ifdef FILEOUTPUT std::ofstream ofs("./in_out/output.txt"); std::cout.rdbuf(ofs.rdbuf()); #endif std::cout << std::setprecision(18) << std::fixed; solve(); std::cout << std::flush; return 0; }