結果
| 問題 |
No.957 植林
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-20 20:15:18 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,445 ms / 2,000 ms |
| コード長 | 2,668 bytes |
| コンパイル時間 | 7,352 ms |
| コンパイル使用メモリ | 267,100 KB |
| 最終ジャッジ日時 | 2025-01-08 13:34:32 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
コンパイルメッセージ
main.cpp: In instantiation of 'void Dinic<MAXV, T>::addEdge(int, int, T, bool) [with int MAXV = 100000; T = long long int]':
main.cpp:86:13: required from here
main.cpp:17:41: warning: narrowing conversion of '((Dinic<100000, long long int>*)this)->Dinic<100000, long long int>::adj[b].std::vector<Dinic<100000, long long int>::edge, std::allocator<Dinic<100000, long long int>::edge> >::size()' from 'std::vector<Dinic<100000, long long int>::edge, std::allocator<Dinic<100000, long long int>::edge> >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
17 | adj[a].push_back({b, adj[b].size(), cap, 0});
| ~~~~~~~~~~~^~
main.cpp:18:44: warning: narrowing conversion of '(((Dinic<100000, long long int>*)this)->Dinic<100000, long long int>::adj[a].std::vector<Dinic<100000, long long int>::edge, std::allocator<Dinic<100000, long long int>::edge> >::size() - 1)' from 'std::vector<Dinic<100000, long long int>::edge, std::allocator<Dinic<100000, long long int>::edge> >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
18 | adj[b].push_back({a, adj[a].size() - 1, isDirected ? 0 : cap, 0});
| ~~~~~~~~~~~~~~^~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
template <int MAXV, class T = int> struct Dinic {
const static bool SCALING = true; // non-scaling = V^2E, Scaling=VElog(U) with higher constant
int lim = 1;
const T INF = numeric_limits<T>::max();
struct edge {
int to, rev;
T cap, flow;
};
int s = MAXV - 2, t = MAXV - 1;
int level[MAXV], ptr[MAXV];
vector<edge> adj[MAXV];
void addEdge(int a, int b, T cap, bool isDirected = true) {
adj[a].push_back({b, adj[b].size(), cap, 0});
adj[b].push_back({a, adj[a].size() - 1, isDirected ? 0 : cap, 0});
}
bool bfs() {
queue<int> q({s});
fill_n(level, MAXV, -1);
level[s] = 0;
while (!q.empty() && level[t] == -1) {
int v = q.front();
q.pop();
for (auto e : adj[v]) {
if (level[e.to] == -1 && e.flow < e.cap && (!SCALING || e.cap - e.flow >= lim)) {
q.push(e.to);
level[e.to] = level[v] + 1;
}
}
}
return level[t] != -1;
}
T dfs(int v, T flow) {
if (v == t || !flow)
return flow;
for (; ptr[v] < adj[v].size(); ptr[v]++) {
edge &e = adj[v][ptr[v]];
if (level[e.to] != level[v] + 1)
continue;
if (T pushed = dfs(e.to, min(flow, e.cap - e.flow))) {
e.flow += pushed;
adj[e.to][e.rev].flow -= pushed;
return pushed;
}
}
return 0;
}
long long calc() {
long long flow = 0;
for (lim = SCALING ? (1 << 30) : 1; lim > 0; lim >>= 1) {
while (bfs()) {
fill_n(ptr, MAXV, 0);
while (T pushed = dfs(s, INF))
flow += pushed;
}
}
return flow;
}
};
int n, m;
int G[305][305];
int R[305];
int C[305];
Dinic<100000, long long> D;
int main()
{
scanf("%d %d", &n, &m);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
scanf("%d", &G[i][j]);
}
}
for(int i = 0;i < n;i++){
scanf("%d", &R[i]);
}
for(int i = 0;i < m;i++){
scanf("%d", &C[i]);
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
D.addEdge(i*m+j, D.t, G[i][j]);
}
}
long long ans = 0;
for(int i = 0;i < n;i++){
D.addEdge(D.s, n*m+i, R[i]);
ans += R[i];
for(int j = 0;j < m;j++){
D.addEdge(n*m+i, i*m+j, R[i]);
}
}
for(int i = 0;i < m;i++){
D.addEdge(D.s, n*m+n+i, C[i]);
ans += C[i];
for(int j = 0;j < n;j++){
D.addEdge(n*m+n+i, j*m+i, C[i]);
}
}
ans -= D.calc();
cout << ans << endl;
return 0;
}