結果
問題 | No.2354 Poor Sight in Winter |
ユーザー | 👑 emthrm |
提出日時 | 2023-06-16 21:39:06 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 242 ms / 2,000 ms |
コード長 | 5,311 bytes |
コンパイル時間 | 3,262 ms |
コンパイル使用メモリ | 262,832 KB |
実行使用メモリ | 15,748 KB |
最終ジャッジ日時 | 2024-06-24 13:25:57 |
合計ジャッジ時間 | 6,471 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 139 ms
15,488 KB |
testcase_12 | AC | 109 ms
15,492 KB |
testcase_13 | AC | 228 ms
15,748 KB |
testcase_14 | AC | 242 ms
15,620 KB |
testcase_15 | AC | 210 ms
15,628 KB |
testcase_16 | AC | 226 ms
15,620 KB |
testcase_17 | AC | 234 ms
15,620 KB |
testcase_18 | AC | 79 ms
9,100 KB |
testcase_19 | AC | 162 ms
13,072 KB |
testcase_20 | AC | 87 ms
9,956 KB |
testcase_21 | AC | 8 ms
6,944 KB |
testcase_22 | AC | 40 ms
6,944 KB |
testcase_23 | AC | 42 ms
6,944 KB |
testcase_24 | AC | 136 ms
12,040 KB |
testcase_25 | AC | 30 ms
6,940 KB |
testcase_26 | AC | 23 ms
6,940 KB |
testcase_27 | AC | 3 ms
6,944 KB |
testcase_28 | AC | 4 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template <typename T, typename U> struct MinimumCostSTFlow { struct Edge { int dst, rev; T cap; U cost; explicit Edge(const int dst, const T cap, const U cost, const int rev) : dst(dst), rev(rev), cap(cap), cost(cost) {} }; const U uinf; std::vector<std::vector<Edge>> graph; explicit MinimumCostSTFlow(const int n, const U uinf = std::numeric_limits<U>::max()) : uinf(uinf), graph(n), tinf(std::numeric_limits<T>::max()), n(n), has_negative_edge(false), prev_v(n, -1), prev_e(n, -1), dist(n), potential(n, 0) {} void add_edge(const int src, const int dst, const T cap, const U cost) { has_negative_edge |= cost < 0; graph[src].emplace_back(dst, cap, cost, graph[dst].size()); graph[dst].emplace_back(src, 0, -cost, graph[src].size() - 1); } U solve(const int s, const int t, T flow) { if (flow == 0) [[unlikely]] return 0; U res = 0; has_negative_edge ? bellman_ford(s) : dijkstra(s); while (true) { if (dist[t] == uinf) return uinf; res += calc(s, t, &flow); if (flow == 0) break; dijkstra(s); } return res; } U solve(const int s, const int t) { U res = 0; T flow = tinf; bellman_ford(s); while (potential[t] < 0 && dist[t] != uinf) { res += calc(s, t, &flow); dijkstra(s); } return res; } std::pair<T, U> minimum_cost_maximum_flow(const int s, const int t, const T flow) { if (flow == 0) [[unlikely]] return {0, 0}; T f = flow; U cost = 0; has_negative_edge ? bellman_ford(s) : dijkstra(s); while (dist[t] != uinf) { cost += calc(s, t, &f); if (f == 0) break; dijkstra(s); } return {flow - f, cost}; } private: const T tinf; const int n; bool has_negative_edge; std::vector<int> prev_v, prev_e; std::vector<U> dist, potential; std::priority_queue<std::pair<U, int>, std::vector<std::pair<U, int>>, std::greater<std::pair<U, int>>> que; void bellman_ford(const int s) { std::fill(dist.begin(), dist.end(), uinf); dist[s] = 0; bool is_updated = true; for (int step = 0; step < n && is_updated; ++step) { is_updated = false; for (int i = 0; i < n; ++i) { if (dist[i] == uinf) continue; for (int j = 0; std::cmp_less(j, graph[i].size()); ++j) { const Edge& e = graph[i][j]; if (e.cap > 0 && dist[e.dst] > dist[i] + e.cost) { dist[e.dst] = dist[i] + e.cost; prev_v[e.dst] = i; prev_e[e.dst] = j; is_updated = true; } } } } assert(!is_updated); for (int i = 0; i < n; ++i) { if (dist[i] != uinf) potential[i] += dist[i]; } } void dijkstra(const int s) { std::fill(dist.begin(), dist.end(), uinf); dist[s] = 0; que.emplace(0, s); while (!que.empty()) { const auto [d, ver] = que.top(); que.pop(); if (dist[ver] < d) continue; for (int i = 0; std::cmp_less(i, graph[ver].size()); ++i) { const Edge& e = graph[ver][i]; const U nxt = dist[ver] + e.cost + potential[ver] - potential[e.dst]; if (e.cap > 0 && dist[e.dst] > nxt) { dist[e.dst] = nxt; prev_v[e.dst] = ver; prev_e[e.dst] = i; que.emplace(dist[e.dst], e.dst); } } } for (int i = 0; i < n; ++i) { if (dist[i] != uinf) potential[i] += dist[i]; } } U calc(const int s, const int t, T* flow) { T f = *flow; for (int v = t; v != s; v = prev_v[v]) { f = std::min(f, graph[prev_v[v]][prev_e[v]].cap); } *flow -= f; for (int v = t; v != s; v = prev_v[v]) { Edge& e = graph[prev_v[v]][prev_e[v]]; e.cap -= f; graph[v][e.rev].cap += f; } return potential[t] * f; } }; int main() { int n, k; cin >> n >> k; vector<int> x(n + 2), y(n + 2); REP(i, n + 2) cin >> x[i] >> y[i]; int lb = 0, ub = abs(x[1] - x[0]) + abs(y[1] - y[0]); while (ub - lb > 1) { const int mid = midpoint(lb, ub); MinimumCostSTFlow<int, ll> flow(n + 2); REP(i, n + 2) REP(j, n + 2) { if (j == i) continue; const int dist = abs(x[j] - x[i]) + abs(y[j] - y[i]); flow.add_edge(i, j, 1, (dist - 1) / mid); } (flow.solve(0, 1, 1) <= k ? ub : lb) = mid; } cout << ub << '\n'; return 0; }