結果
問題 | No.2713 Just Solitaire |
ユーザー | noya2 |
提出日時 | 2024-04-02 17:28:14 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 17,878 bytes |
コンパイル時間 | 2,950 ms |
コンパイル使用メモリ | 268,600 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-09-30 23:15:36 |
合計ジャッジ時間 | 3,854 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
testcase_10 | AC | 1 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 2 ms
5,248 KB |
testcase_13 | AC | 2 ms
5,248 KB |
testcase_14 | AC | 1 ms
5,248 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 2 ms
5,248 KB |
testcase_17 | AC | 1 ms
5,248 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 2 ms
5,248 KB |
testcase_20 | AC | 1 ms
5,248 KB |
testcase_21 | AC | 1 ms
5,248 KB |
testcase_22 | AC | 1 ms
5,248 KB |
testcase_23 | AC | 1 ms
5,248 KB |
testcase_24 | AC | 3 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 3 ms
5,248 KB |
testcase_29 | AC | 3 ms
5,248 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 2 ms
5,248 KB |
testcase_32 | AC | 2 ms
5,248 KB |
testcase_33 | AC | 3 ms
5,248 KB |
ソースコード
#line 2 "/Users/noya2/Desktop/Noya2_library/template/template.hpp" using namespace std; #include<bits/stdc++.h> #line 1 "/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp" namespace noya2 { template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p){ os << p.first << " " << p.second; return os; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p){ is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v){ int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; } void in() {} template <typename T, class... U> void in(T &t, U &...u){ cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U, char sep = ' '> void out(const T &t, const U &...u){ cout << t; if (sizeof...(u)) cout << sep; out(u...); } template<typename T> void out(const vector<vector<T>> &vv){ int s = (int)vv.size(); for (int i = 0; i < s; i++) out(vv[i]); } struct IoSetup { IoSetup(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7); } } iosetup_noya2; } // namespace noya2 #line 1 "/Users/noya2/Desktop/Noya2_library/template/const.hpp" namespace noya2{ const int iinf = 1'000'000'007; const long long linf = 2'000'000'000'000'000'000LL; const long long mod998 = 998244353; const long long mod107 = 1000000007; const long double pi = 3.14159265358979323; const vector<int> dx = {0,1,0,-1,1,1,-1,-1}; const vector<int> dy = {1,0,-1,0,1,-1,-1,1}; const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string alp = "abcdefghijklmnopqrstuvwxyz"; const string NUM = "0123456789"; void yes(){ cout << "Yes\n"; } void no(){ cout << "No\n"; } void YES(){ cout << "YES\n"; } void NO(){ cout << "NO\n"; } void yn(bool t){ t ? yes() : no(); } void YN(bool t){ t ? YES() : NO(); } } // namespace noya2 #line 1 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp" namespace noya2{ unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){ if (a == 0 || b == 0) return a + b; int n = __builtin_ctzll(a); a >>= n; int m = __builtin_ctzll(b); b >>= m; while (a != b) { int mm = __builtin_ctzll(a - b); bool f = a > b; unsigned long long c = f ? a : b; b = f ? b : a; a = (c - b) >> mm; } return a << min(n, m); } template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); } long long sqrt_fast(long long n) { if (n <= 0) return 0; long long x = sqrt(n); while ((x + 1) * (x + 1) <= n) x++; while (x * x > n) x--; return x; } template<typename T> T floor_div(const T n, const T d) { assert(d != 0); return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0); } template<typename T> T ceil_div(const T n, const T d) { assert(d != 0); return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0); } template<typename T> void uniq(vector<T> &v){ sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); } template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; } } // namespace noya2 #line 8 "/Users/noya2/Desktop/Noya2_library/template/template.hpp" #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define repp(i,m,n) for (int i = (m); i < (int)(n); i++) #define reb(i,n) for (int i = (int)(n-1); i >= 0; i--) #define all(v) (v).begin(),(v).end() using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using pil = pair<int,ll>; using pli = pair<ll,int>; namespace noya2{ /* ~ (. _________ . /) */ } using namespace noya2; #line 2 "c.cpp" #line 2 "/Users/noya2/Desktop/Noya2_library/flow/project_selection.hpp" #line 2 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" #line 2 "/Users/noya2/Desktop/Noya2_library/data_structure/simple_queue.hpp" #line 4 "/Users/noya2/Desktop/Noya2_library/data_structure/simple_queue.hpp" namespace noya2::internal { template <class T> struct simple_queue { std::vector<T> payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const T& t) { payload.push_back(t); } T& front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; } // namespace noya2::internal #line 4 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" #line 10 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" namespace noya2 { template <class Cap> struct mf_graph { public: mf_graph() : _n(0) {} explicit mf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap}); g[to].push_back(_edge{from, from_id, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto& _e = g[pos[i].first][pos[i].second]; auto& _re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector<int> level(_n), iter(_n); internal::simple_queue<int> que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } }; auto dfs = [&](auto self, int v, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int& i = iter[v]; i < int(g[v].size()); i++) { _edge& e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) return res; } level[v] = _n; return res; }; Cap flow = 0; while (flow < flow_limit) { bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } return flow; } std::vector<bool> min_cut(int s) { std::vector<bool> visited(_n); internal::simple_queue<int> que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } private: int _n; struct _edge { int to, rev; Cap cap; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; } // namespace noya2 #line 2 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp" #line 4 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp" #include<ranges> #line 7 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp" namespace noya2::internal { template<class E> struct csr final { csr () {} csr (int _n) : n(_n) {} csr (int _n, int m) : n(_n){ start.reserve(m); elist.reserve(m); } // ACL style constructor (do not have to call build) csr (int _n, const std::vector<std::pair<int,E>> &idx_elem) : n(_n), start(_n + 2), elist(idx_elem.size()) { for (auto &[i, e] : idx_elem){ start[i + 2]++; } for (int i = 1; i < n; i++){ start[i + 2] += start[i + 1]; } for (auto &[i, e] : idx_elem){ elist[start[i + 1]++] = e; } prepared = true; } int add(int idx, E elem){ int eid = start.size(); start.emplace_back(idx); elist.emplace_back(elem); return eid; } void build(){ if (prepared) return ; int m = start.size(); std::vector<E> nelist(m); std::vector<int> nstart(n + 2, 0); for (int i = 0; i < m; i++){ nstart[start[i] + 2]++; } for (int i = 1; i < n; i++){ nstart[i + 2] += nstart[i + 1]; } for (int i = 0; i < m; i++){ nelist[nstart[start[i] + 1]++] = elist[i]; } swap(elist,nelist); swap(start,nstart); prepared = true; } const auto operator[](int idx) const { return std::ranges::subrange(elist.begin()+start[idx],elist.begin()+start[idx+1]); } auto operator[](int idx){ return std::ranges::subrange(elist.begin()+start[idx],elist.begin()+start[idx+1]); } const auto operator()(int idx, int l, int r) const { return std::ranges::subrange(elist.begin()+start[idx]+l,elist.begin()+start[idx]+r); } auto operator()(int idx, int l, int r){ return std::ranges::subrange(elist.begin()+start[idx]+l,elist.begin()+start[idx]+r); } int n; std::vector<int> start; std::vector<E> elist; bool prepared = false; }; } // namespace noya2::internal #line 151 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" namespace noya2 { template<class Cap> struct maxflow { struct edge { int from, to; Cap cap, flow; }; maxflow() : n(0) {} explicit maxflow(int _n) : n(_n), g(n), degree(_n,0) {} int add_edge(int from, int to, Cap cap){ assert(0 <= from && from < n); assert(0 <= to && to < n); assert(0 <= cap); int eid = (int)(pos.size()); int from_id = degree[from]; int to_id = degree[to] + (from == to ? 1 : 0); g.add(from, _edge{to, to_id, cap}); degree[from]++; g.add(to, _edge{from, from_id, 0}); degree[to]++; pos.emplace_back(from, from_id); return eid; } edge get_edge(int i) { auto e = g[pos[i].first][pos[i].second]; auto re = g[e.to][e.rev]; return edge{pos[i].first, e.to, e.cap + re.cap, re.cap}; } Cap flow(int s, int t){ return flow(s, t, std::numeric_limits<Cap>::max()); } Cap flow(int s, int t, Cap flow_limit){ assert(0 <= s && s < n); assert(0 <= t && t < n); assert(s != t); g.build(); std::vector<int> level(n), iter(n); internal::simple_queue<int> que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()){ int v = que.front(); que.pop(); for (auto e : g[v]){ if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return ; que.push(e.to); } } }; auto dfs = [&](auto sfs, int v, Cap up){ if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int &i = iter[v]; i < degree[v]; i++){ _edge &e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = sfs(sfs, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) return res; } level[v] = n; return res; }; Cap flow = 0; while (flow < flow_limit){ bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } return flow; } std::vector<bool> min_cut(int s){ std::vector<bool> visited(n); internal::simple_queue<int> que; que.push(s); visited[s] = true; while (!que.empty()){ int v = que.front(); que.pop(); for (auto e : g[v]){ if (e.cap && !visited[e.to]){ visited[e.to] = true; que.push(e.to); } } } return visited; } private: int n; struct _edge { int to, rev; Cap cap; }; std::vector<std::pair<int, int>> pos; internal::csr<_edge> g; std::vector<int> degree; }; } // namespace noya2 #line 4 "/Users/noya2/Desktop/Noya2_library/flow/project_selection.hpp" #line 6 "/Users/noya2/Desktop/Noya2_library/flow/project_selection.hpp" namespace noya2 { template<class Cost> struct project_selection { int n; // Costs c0 without any constraints. Cost c0; // It costs c1[ 2*i + x_i ]. std::vector<Cost> c1; maxflow<Cost> mfg; project_selection () {} project_selection (int _n) : n(_n), c0(0), c1(_n*2, 0), mfg(n+2) {} void cost_total(Cost cost){ c0 += cost; } void profit_total(Cost profit){ c0 -= profit; } void cost_x0(int x, Cost cost){ c1[2*x + 0] += cost; } void cost_x1(int x, Cost cost){ c1[2*x + 1] += cost; } void cost_xp(int x, std::array<Cost,2> cost){ c1[2*x + 0] += cost[0]; c1[2*x + 1] += cost[1]; } void profit_x0(int x, Cost profit){ c1[2*x + 0] -= profit; } void profit_x1(int x, Cost profit){ c1[2*x + 1] -= profit; } void profit_xp(int x, std::array<Cost,2> profit){ c1[2*x + 0] -= profit[0]; c1[2*x + 1] -= profit[1]; } void cost_x0y1(int x, int y, Cost c){ assert(c >= 0); mfg.add_edge(x, y, c); } void cost_x1y0(int x, int y, Cost c){ cost_x0y1(y, x, c); } void cost_xpyq(int x, int y, std::array<std::array<Cost,2>,2> cost){ c0 += cost[0][0]; cost_x1(x, cost[1][0] - cost[0][0]); cost_x1(y, cost[1][1] - cost[1][0]); cost_x0y1(x, y, cost[1][0] + cost[0][1] - cost[0][0] - cost[1][1]); } void profit_x0y0(int x, int y, Cost profit){ profit_total(profit); cost_x1(x, profit); cost_x0y1(x, y, profit); } void profit_x1y1(int x, int y, Cost profit){ profit_x1(y, profit); cost_x0y1(x, y, profit); } Cost min_cost(){ int s = n, t = n+1; for (int x = 0; x < n; x++){ Cost x0 = c1[2*x + 0]; Cost x1 = c1[2*x + 1]; c0 += std::min(x0, x1); if (x0 < x1){ mfg.add_edge(s, x, x1 - x0); } if (x0 > x1){ mfg.add_edge(x, t, x0 - x1); } } Cost ans = c0 + mfg.flow(s, t); return ans; } Cost max_profit(){ return -min_cost(); } std::vector<bool> answer(){ auto ans = mfg.min_cut(n); ans.pop_back(); // pop s ans.pop_back(); // pop t for (auto &x : ans){ x = !x; } return ans; } }; } // namespace noya2 #line 4 "c.cpp" void solve(){ int n, m; in(n,m); vector<ll> a(n), b(m); in(a,b); project_selection<ll> ps(n+m); rep(i,m){ int k; in(k); while (k--){ int x; in(x); x--; ps.cost_x0y1(x, n+i, iinf); } ps.profit_x1(n+i, b[i]); } rep(i,n){ ps.cost_x1(i, a[i]); } out(ps.max_profit()); } int main(){ int t = 1; //in(t); while (t--) { solve(); } }