QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#110710 | #4775. Pool construction | Zeardoe | AC ✓ | 34ms | 4180kb | C++20 | 2.3kb | 2023-06-03 14:19:20 | 2023-06-03 14:19:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2510, MAXM = 25010, INF = 1e9;
struct MaxFlow {
int S, T, head[MAXN], cur[MAXN], dis[MAXN], cnt = 1;
bool vis[MAXN];
struct edge {int v, c, nxt;} e[MAXM * 2];
void add(int u, int v, int c) {
e[++cnt] = {v, c, head[u]}, head[u] = cnt;
e[++cnt] = {u, 0, head[v]}, head[v] = cnt;
}
void rebuild() {
for (int i = 1; i <= cnt; i += 2)
e[i].c += e[i ^ 1].c, e[i ^ 1].c = 0;
}
void clear() {
memset(head, 0, sizeof(head));
cnt = 1;
}
bool bfs() {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
memcpy(cur, head, sizeof(head));
queue<int> q; q.push(S), vis[S] = 1, dis[S] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = head[u]; i; i = e[i].nxt) {
if (!e[i].c || vis[e[i].v]) continue;
dis[e[i].v] = dis[u] + 1, vis[e[i].v] = 1;
q.push(e[i].v);
}
}
return vis[T];
}
int dfs(int now, int flow) {
if(now == T) return flow;
int res = 0;
for(int i = cur[now]; i && res < flow; i = e[i].nxt) {
if(!e[i].c || dis[e[i].v] != dis[now] + 1) continue;
int tmp = dfs(e[i].v, min(flow - res, e[i].c));
e[i].c -= tmp, e[i ^ 1].c += tmp, res += tmp;
if(res < flow) cur[now] = e[i].nxt;
}
return res;
}
int solve() {
int ans = 0;
while (bfs()) ans += dfs(S, INF);
return ans;
}
} G;
string s[55];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
void Solve(int test) {
G.clear();
int n, m;
int d, f, b;
cin >> m >> n >> d >> f >> b;
G.S = 0, G.T = n * m + 1;
for (int i = 1; i <= n; i++) {
cin >> s[i];
s[i] = " " + s[i];
for (int j = 1; j <= m; j++) {
if (s[i][j] == '#') G.add(G.S, (i - 1) * m + j, d);
else G.add((i - 1) * m + j, G.T, f);
if (i == 1 || j == 1 || i == n || j == m)
G.add(G.S, (i - 1) * m + j, INF);
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
G.add((i - 1) * m + j, (nx - 1) * m + ny, b);
}
cout << G.solve() << endl;
}
signed main() {
int T;
cin >> T;
for (int _test = 1; _test <= T; _test++)
Solve(_test);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3580kb
input:
3 3 3 5 5 1 #.# #.# ### 5 4 1 8 1 #..## ##.## #.#.# ##### 2 2 27 11 11 #. .#
output:
9 27 22
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 34ms
memory: 4180kb
input:
56 3 3 5 5 1 #.# #.# ### 5 4 1 8 1 #..## ##.## #.#.# ##### 2 2 1 1 1 ## ## 2 2 1 10000 1 .. .. 5 4 20 41 9 ##### ##.## #.#.# ##### 5 4 20 41 10 ##### ##.## #.#.# ##### 5 4 20 41 11 ##### ##.## #.#.# ##### 5 4 20 39 10 ##### ##.## #.#.# ##### 3 3 9760 9015 711 .#. #.# ### 5 5 7415 7931 2080 ..... #.....
output:
9 27 0 40000 108 120 123 117 20874 100110 112364 203900 271440 462119 490330 1746528 1067774 1055196 2609818 2094932 5199902 13978 73960 99018 262976 224632 78984 167795 392774 649054 1232290 135876 318982 413042 1479538 1680354 349557 540100 2101110 335884 2245998 170698 780013 1804351 2998519 3661...
result:
ok 56 lines