結果
問題 | No.2713 Just Solitaire |
ユーザー | MitI_7 |
提出日時 | 2024-12-12 16:12:50 |
言語 | C++23(gcc13) (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 5 ms / 2,000 ms |
コード長 | 8,749 bytes |
コンパイル時間 | 2,711 ms |
コンパイル使用メモリ | 186,120 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-12 16:12:58 |
合計ジャッジ時間 | 4,220 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 3 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 3 ms
5,248 KB |
testcase_13 | AC | 3 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 2 ms
5,248 KB |
testcase_17 | AC | 2 ms
5,248 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 2 ms
5,248 KB |
testcase_20 | AC | 2 ms
5,248 KB |
testcase_21 | AC | 2 ms
5,248 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 3 ms
5,248 KB |
testcase_25 | AC | 3 ms
5,248 KB |
testcase_26 | AC | 3 ms
5,248 KB |
testcase_27 | AC | 4 ms
5,248 KB |
testcase_28 | AC | 3 ms
5,248 KB |
testcase_29 | AC | 3 ms
5,248 KB |
testcase_30 | AC | 3 ms
5,248 KB |
testcase_31 | AC | 3 ms
5,248 KB |
testcase_32 | AC | 3 ms
5,248 KB |
testcase_33 | AC | 5 ms
5,248 KB |
ソースコード
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <map> #include <memory> #include <numeric> #include <iomanip> #include <iostream> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> #include <random> #define LEN(x) (long long)(x.size()) #define FOR(i, a, n) for(int i=(a);i<(n); ++i) #define FOE(i, a) for(auto i : a) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define BIT_COUNT32(bit) (__builtin_popcount(bit)) #define BIT_COUNT64(bit) (__builtin_popcountll(bit)) template<typename T> using MinPriorityQueue = std::priority_queue<T, std::vector<T>, std::greater<T> >; template<typename T> using MaxPriorityQueue = std::priority_queue<T>; // @formatter:off typedef long long LL; typedef __int128_t LLL; template<typename T> std::vector<T> make_v(size_t a){return std::vector<T>(a);} template<typename T,typename... Ts> auto make_v(size_t a, Ts... ts){ return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));} // C++14 template<typename T,typename V> typename std::enable_if<std::is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;} template<typename T,typename V> typename std::enable_if<std::is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);} template<class T> inline T ceil(T a, T b) { assert(a >= 0 and b > 0); return (a + b - 1) / b; } void print() { std::cout << std::endl; } template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { std::cout << head; if (sizeof...(tail) != 0) {std::cout << " ";} print(std::forward<Tail>(tail)...); } template <class T> void print(std::vector<T> &v) {for (auto& a : v) { std::cout << a; if (&a != &v.back()) {std::cout << " ";} }std::cout << std::endl;} template <class T> void print(std::pair<T, T> &p) { std::cout << p.first << " " << p.second << std::endl; } void debug() { std::cerr << std::endl; } template <class Head, class... Tail> void debug(Head&& head, Tail&&... tail) { std::cerr << head; if (sizeof...(tail) != 0) {std::cerr << " ";} debug(std::forward<Tail>(tail)...); } template <class T> void debug(std::vector<T> &v) {for (auto& a : v) { std::cerr << a; if (&a != &v.back()) {std::cerr << " ";} }std::cerr << std::endl;} template <class T> void debug(std::pair<T, T> &p) { std::cerr << p.first << " " << p.second << std::endl; } inline bool inside(long long y, long long x, long long H, long long W) {return 0 <= y and y < H and 0 <= x and x < W; } template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } template<class T> inline T euclidean_distance2(T y1, T x1, T y2, T x2) { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); } template<class T> inline T manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); } template<typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); } template<typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); } bool is_bit_on(const unsigned long long bit, const unsigned int i) { return (bit >> i) & 1u; } unsigned long long get_bit_set(const unsigned long long bit, const unsigned int i, const unsigned int b) { assert(b == 0 or b == 1); if (b == 0) { return bit & ~(1ull << i); } else {return bit | (1ull << i);}} // 初項s交差d長さnの数列の和 long long sum_of_arithmetic_progression(long long s, long long d, long long n) { return n * (2 * s + (n - 1) * d) / 2; } // 三角数 long long triangular_number(long long n) { return n * (n + 1) / 2; } // sqrt(x)の整数解を求める // 整数解がなければ-1 long long sqrt_integer(const long long x) { if (x < 0) { return -1; } auto a = (long long)sqrt(x); if (a * a == x) { return a; } if((a - 1) * (a - 1) == x) { return a - 1; } if((a + 1) * (a + 1) == x) { return a + 1; } return -1; } // xが2の階乗かどうか判定 bool is_power_of_two(long long x) { return !(x & (x - 1)); } // O(log max(a, b_sum)) long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } long long lcm(long long a, long long b) { long long g = gcd(a, b); return a / g * b; } const int INF = 1u << 30u; // 1,073,741,824 const long long LINF = 1ull << 60u; const double EPS = 1e-9; const long double PI = acos(-1.0); // 2次元配列上での移動.右,下,左,上,右上,右下,左下,左上 const std::vector<int> dy8 = {0, 1, 0, -1, -1, 1, 1, -1}, dx8 = {1, 0, -1, 0, 1, 1, -1, -1}; // @formatter:on using namespace std; #include <cassert> #include <cmath> #include <queue> #include <vector> // 最大流問題を解く O(|E||V|^2) class Dinic { public: struct Edge { const unsigned int to; // 行き先のノードid long long flow; // 流量 const long long cap; // 容量 const unsigned int rev; // 逆辺のノードid const bool is_rev; // 逆辺かどうか Edge(int to, long long flow, long long cap, int rev, bool is_rev) : to(to), flow(flow), cap(cap), rev(rev), is_rev(is_rev) { assert(this->cap >= 0); } }; std::vector<std::vector<Edge> > graph; // グラフの隣接リスト表現 std::vector<int> level; // sからの距離 std::vector<unsigned int> iter; // どこまで調べ終わったか Dinic(unsigned int num_of_node) { assert(num_of_node > 0); this->graph.resize(num_of_node); this->level.resize(num_of_node); this->iter.resize(num_of_node); } // fromからtoへ向かう容量capの辺をグラフに追加する void add_edge(unsigned int from, unsigned int to, long long cap) { this->graph[from].emplace_back(Edge(to, 0, cap, (unsigned int) graph[to].size(), false)); this->graph[to].emplace_back(Edge(from, cap, cap, (unsigned int) graph[from].size() - 1, true)); } // sからtへの最大流を求める long long max_flow(unsigned int s, unsigned int t) { long long flow = 0; while (true) { this->bfs(s); if (this->level[t] < 0) { return flow; } fill(this->iter.begin(), this->iter.end(), 0); long long f; while ((f = dfs(s, t, 10000000000ll)) > 0) { flow += f; } } } private: // sからの最短距離をBFSで計算する void bfs(unsigned int s) { fill(this->level.begin(), this->level.end(), -1); std::queue<unsigned int> que; this->level[s] = 0; que.push(s); while (not que.empty()) { unsigned int v = que.front(); que.pop(); for (int i = 0; i < (int) this->graph[v].size(); ++i) { Edge &e = this->graph[v][i]; if ((e.cap - e.flow) > 0 and level[e.to] < 0) { this->level[e.to] = this->level[v] + 1; que.push(e.to); } } } } // 増加パスをDFSで探す long long dfs(unsigned int v, unsigned int t, long long f) { if (v == t) { return f; } for (unsigned int &i = this->iter[v]; i < this->graph[v].size(); ++i) { Edge &e = this->graph[v][i]; if ((e.cap - e.flow) > 0 and this->level[v] < this->level[e.to]) { long long d = dfs(e.to, t, std::min(f, e.cap - e.flow)); if (d > 0) { e.flow += d; this->graph[e.to][e.rev].flow -= d; return d; } } } return 0; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<LL> A(N); FOR(i, 0, N) { cin >> A[i]; } vector<LL> B(M); FOR(i, 0, M) { cin >> B[i]; } auto C = make_v<LL>(M, 0); FOR(i, 0, M) { int K; cin >> K; FOR(j, 0, K) { LL c; cin >> c; c--; C[i].emplace_back(c); } } Dinic dinic(N + M + 2); int s = N + M; int t = s + 1; FOR(i, 0, N) { dinic.add_edge(i, t, A[i]); } LL ans = 0; FOR(i, 0, M) { ans += B[i]; dinic.add_edge(s, N + i, B[i]); } FOR(i, 0, M) { FOR(j, 0, LEN(C[i])) { dinic.add_edge(N + i, C[i][j], INF); } } ans -= dinic.max_flow(s, t); print(ans); return 0; }