結果
| 問題 | No.957 植林 |
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2019-12-19 22:14:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,437 bytes |
| 記録 | |
| コンパイル時間 | 2,184 ms |
| コンパイル使用メモリ | 182,976 KB |
| 実行使用メモリ | 24,620 KB |
| 最終ジャッジ日時 | 2024-07-07 01:53:10 |
| 合計ジャッジ時間 | 6,837 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 15 TLE * 1 -- * 29 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<class T> struct Dinic {
struct Edge { int to, rev; T cap; };
const T inf = numeric_limits<T>::max();
const int n;
vector< vector<Edge> > g;
vector<int> dist, i;
Dinic(int _n) : n(_n), g(n), dist(n), i(n) {}
void add_edge(int from, int to, T cap) {
assert(from != to);
assert(cap >= 0);
if (!cap) return;
g[from].emplace_back(Edge{to, (int)g[to].size(), cap});
g[to].emplace_back(Edge{from, (int)g[from].size() - 1, 0});
}
void bfs(int s) {
fill(begin(dist), end(dist), -1);
queue<int> que;
dist[s] = 0;
que.push(s);
while (!que.empty()) {
int v = que.front(); que.pop();
for (const auto& e : g[v]) {
if (dist[e.to] != -1 or !e.cap) continue;
dist[e.to] = dist[v] + 1;
que.push(e.to);
}
}
}
T dfs(int v, int s, T f) {
if (v == s) return f;
for (; i[v] < (int)g[v].size(); ++i[v]) {
Edge& e = g[v][i[v]];
if (dist[e.to] >= dist[v] or !g[e.to][e.rev].cap) continue;
T d = dfs(e.to, s, min(f, g[e.to][e.rev].cap));
if (d > 0) {
g[e.to][e.rev].cap -= d;
e.cap += d;
return d;
}
}
return 0;
}
T max_flow(int s, int t) {
assert(s != t);
T res = 0;
while (true) {
bfs(s);
if (dist[t] == -1) return res;
fill(begin(i), end(i), 0);
while (true) {
T f = dfs(t, s, inf);
if (!f) break;
res += f;
}
}
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
int s = h * w + h + w, t = s + 1;
Dinic<long long> g(t + 1);
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
vector<int> is(h), js(w);
iota(begin(is), end(is), 0);
shuffle(begin(is), end(is), mt);
iota(begin(js), end(js), 0);
shuffle(begin(js), end(js), mt);
for (int i : is) {
for (int j : js) {
int a;
cin >> a;
g.add_edge(i * w + j, t, a);
}
}
long long res = 0;
for (int i : is) {
int a;
cin >> a;
res += a;
g.add_edge(s, h * w + i, a);
for (int j : js) {
g.add_edge(h * w + i, i * w + j, g.inf);
}
}
for (int j : js) {
int a;
cin >> a;
res += a;
g.add_edge(s, h * w + h + j, a);
for (int i : is) {
g.add_edge(h * w + h + j, i * w + j, g.inf);
}
}
res -= g.max_flow(s, t);
cout << res << '\n';
}
risujiroh