結果
問題 | No.177 制作進行の宮森あおいです! |
ユーザー | pekempey |
提出日時 | 2017-02-08 18:08:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 5,037 bytes |
コンパイル時間 | 2,389 ms |
コンパイル使用メモリ | 191,012 KB |
実行使用メモリ | 35,840 KB |
最終ジャッジ日時 | 2024-06-07 13:08:52 |
合計ジャッジ時間 | 6,834 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
10,752 KB |
testcase_01 | AC | 7 ms
5,376 KB |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 46 ms
5,504 KB |
testcase_04 | AC | 49 ms
5,504 KB |
testcase_05 | AC | 900 ms
15,744 KB |
testcase_06 | TLE | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
ソースコード
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-6; void replace_basis(vector<vector<double>> &a, vector<int> &id, int k, int j) { double r = a[k][j]; for (int i = 0; i < a[0].size(); i++) a[k][i] /= r; a[k][j] = 1; for (int i = 0; i < a.size(); i++) if (i != k) { double r = a[i][j]; for (int j = 0; j < a[i].size(); j++) a[i][j] -= a[k][j] * r; a[i][j] = 0; } id[k] = j; } void simplex(vector<vector<double>> &a, vector<int> &id) { const int M = a.size() - 1; while (true) { int j = max_element(a[M].begin() + 1, a[M].end()) - a[M].begin(); if (a[M][j] < EPS) break; pair<double, int> mn(1e100, INT_MAX); for (int i = 0; i < M; i++) { if (a[i][j] > EPS) mn = min(mn, make_pair(a[i][0] / a[i][j], i)); } int k = mn.second; replace_basis(a, id, k, j); } } // one-phase simplex double simplex1(vector<vector<double>> a, vector<double> b, vector<double> c) { const int M = a.size(); const int N = a[0].size(); vector<vector<double>> table(M + 1, vector<double>(N + M + 1)); for (int i = 0; i < M; i++) { table[i][0] = b[i]; for (int j = 0; j < N; j++) { table[i][j + 1] = a[i][j]; } table[i][N + i + 1] = 1; } for (int i = 0; i < N; i++) table[M][i + 1] = c[i]; vector<int> id(M); for (int i = 0; i < M; i++) id[i] = N + i + 1; simplex(table, id); return -table[M][0]; } // two-phase simplex // max sum c[i]*x[i] // s.t. sum(a[i][*]*x[i])<=b[i] double simplex(vector<vector<double>> a, vector<double> b, vector<double> c) { const int M = a.size(); const int N = a[0].size(); vector<vector<double>> table(M + 1, vector<double>(N + M + 1)); for (int i = 0; i < M; i++) { table[i][0] = b[i]; for (int j = 0; j < N; j++) { table[i][j + 1] = a[i][j]; table[M][j + 1] += a[i][j]; } table[M][0] += b[i]; table[i][N + i + 1] = 1; } vector<int> id(M); for (int i = 0; i < M; i++) id[i] = N + i + 1; simplex(table, id); for (int i = 0; i < M; i++) { if (id[i] >= N + 1) { int j; for (j = 1; j <= N; j++) { if (abs(table[i][j]) > EPS) break; } if (j == N + 1) return -1e100; replace_basis(table, id, i, j); } } for (int i = 0; i <= M; i++) { table[i].resize(N + 1); } table[M][0] = 0; for (int i = 0; i < N; i++) { table[M][i + 1] = c[i]; } for (int i = 0; i < M; i++) { double r = table[M][id[i]]; for (int j = 0; j <= N; j++) { table[M][j] -= table[i][j] * r; } } simplex(table, id); return -table[M][0]; } // one-phase simplex struct MaxFlowLP2 { int n; vector<int> cap; vector<vector<int>> in, out; int es; MaxFlowLP2(int n) : n(n), in(n), out(n), es(0) { } void add(int u, int v, int c) { cap.push_back(c); out[u].push_back(es); in[v].push_back(es); es++; } int calc(int s, int t) { vector<vector<double>> a; vector<double> b; vector<double> c(es); for (int i = 0; i < es; i++) { vector<double> t(es); t[i] = 1; a.push_back(t); b.push_back(cap[i]); } for (int i = 0; i < n; i++) { if (i != s && i != t) { vector<double> t(es), tt(es); for (int j : out[i]) t[j]++, tt[j]--; for (int j : in[i]) t[j]--, tt[j]++; a.push_back(t); b.push_back(EPS); a.push_back(tt); b.push_back(EPS); } } for (int j : out[s]) c[j]++; for (int j : in[s]) c[j]--; return round(simplex1(a, b, c)); } }; // two-phase simplex struct MaxFlowLP { int n; vector<int> cap; vector<vector<int>> in, out; int es; MaxFlowLP(int n) : n(n), in(n), out(n), es(0) { } void add(int u, int v, int c) { cap.push_back(c); out[u].push_back(es); in[v].push_back(es); es++; } int calc(int s, int t) { vector<vector<double>> a; vector<double> b; vector<double> c(es * 2); for (int i = 0; i < es; i++) { vector<double> t(es * 2); t[i] = t[i + es] = 1; a.push_back(t); b.push_back(cap[i]); } for (int i = 0; i < n; i++) { if (i != s && i != t && (!in[i].empty() || !out[i].empty())) { vector<double> t(es * 2); for (int j : out[i]) t[j]++; for (int j : in[i]) t[j]--; a.push_back(t); b.push_back(0); } } for (int j : out[s]) c[j]++; for (int j : in[s]) c[j]--; return round(simplex(a, b, c)); } }; int main() { int w; cin >> w; int n; cin >> n; vector<int> J(n); for (int i = 0; i < n; i++) { scanf("%d", &J[i]); } int m; cin >> m; vector<int> c(m); for (int i = 0; i < m; i++) { scanf("%d", &c[i]); } vector<vector<bool>> bad(n, vector<bool>(m)); for (int i = 0; i < m; i++) { int q; cin >> q; for (int j = 0; j < q; j++) { int x; cin >> x; x--; bad[x][i] = true; } } MaxFlowLP mf(n + m + 2); int src = n + m; int dst = src + 1; for (int i = 0; i < n; i++) { mf.add(src, i, J[i]); } for (int i = 0; i < m; i++) { mf.add(i + n, dst, c[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!bad[i][j]) { mf.add(i, j + n, 10101010); } } } int f = mf.calc(src, dst); if (f >= w) { cout << "SHIROBAKO" << endl; } else { cout << "BANSAKUTSUKITA" << endl; } }