QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#624609 | #9224. Express Eviction | rikka_lyly | WA | 1ms | 5292kb | C++20 | 3.9kb | 2024-10-09 16:14:30 | 2024-10-09 16:14:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define MAXN 3010
#define MAXE 2 * 30010
#define INF 0x3f3f3f3f3f3f3f3f
struct Edge
{
int to, next = 0; //用STL会MLE,所以用链式前向星
ll cap, cost;
};
int head[MAXN] = {0};
Edge edge[MAXE];
int tot = 2; // 从2开始,配对表示正/反边,不用0/1
void add(int from, int to, ll cap, ll cost)
{
edge[tot].to = to;
edge[tot].cap = cap;
edge[tot].cost = cost;
edge[tot].next = head[from];
head[from] = tot;
tot++;
}
void addline(int from, int to, ll cap, ll cost)
{
add(from, to, cap, cost);
add(to, from, 0, -cost);
}
int N, S, T;
ll maxf[MAXN];
// int pre[MAXN];//存前驱边,每次bfs后跟着前驱边能从t走到s
int deep[MAXN];
int curhead[MAXN];
bool bfs_f()//不能dfs会被卡掉
{
memset(deep, 0, sizeof(deep));
deep[S] = 1;
queue<int> q;
q.push(S);
while (!q.empty())
{
int cur = q.front();
q.pop();
for (int i = head[cur]; i; i = edge[i].next)
{
int to = edge[i].to;
ll cap = edge[i].cap;
if(!deep[to] && cap)
{
deep[to] = deep[cur] + 1;
q.push(to);
if(to == T)
return true;
}
}
}
return false;
}
ll dfs_f(int cur, ll mf)
{
if(cur == T)
return mf;
ll sum = 0;
for (int i = curhead[cur]; i; i = edge[i].next)
{
curhead[cur] = i;//当前弧优化
int to = edge[i].to;
ll cap = edge[i].cap;
if (deep[to] == deep[cur] + 1 && cap)
{
ll f = dfs_f(to, min(mf, cap));
edge[i].cap -= f;
edge[i ^ 1].cap += f;
sum += f;
mf -= f;
if(!mf)//流量优化
break;
}
}
if(!sum)//残枝优化
deep[cur] = 0;
return sum;
}
ll dinic()
{
ll flow = 0;
while (bfs_f())
{
memcpy(curhead, head, sizeof(curhead));
flow += dfs_f(S, INF);
}
return flow;
}
void solve()
{
int n, m;
cin >> n >> m;
vector<string> a(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] = ' ' + a[i];
}
auto trans = [&](int x, int y) -> int
{
return x * (m + 2) + y;
};
S = trans(n + 1, m + 1) + 1, T = S + 1, N = T;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if(1)
{
int val = 0;
if(a[i][j] == '#')
val++;
if(i != 0 && a[i - 1][j] == '#')
val++;
addline(trans(i, j), trans(i - 1, j), val, 0);
}
if(1)
{
int val = 0;
if(a[i][j] == '#')
val++;
if(j != 0 && a[i][j - 1] == '#')
val++;
addline(trans(i, j - 1), trans(i, j), val, 0);
}
}
}
for (int i = 1; i <= n; i++)
{
int val = 0;
if(a[i][m] == '#')
val++;
addline(trans(i, m), trans(i, m + 1), val, 0);
addline(S, trans(i, 0), INF, 0);
addline(trans(i, m + 1), T, INF, 0);
}
for (int j = 1; j <= m; j++)
{
int val = 0;
if(a[n][j] == '#')
val++;
addline(trans(j, n + 1), trans(j, n), val, 0);
addline(S, trans(n + 1, j), INF, 0);
addline(trans(0, j), T, INF, 0);
}
int ans = dinic();
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5292kb
input:
4 6 .##... .#.... ##.... ....#.
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 4932kb
input:
20 30 ...########################### #..########################### ...########################### ...########################### .############################# ...########################### ...########################### #..########################### ...########################### ..#...............
output:
3
result:
wrong answer 1st numbers differ - expected: '11', found: '3'