QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#625029 | #9224. Express Eviction | rikka_lyly | WA | 2ms | 8364kb | C++20 | 3.8kb | 2024-10-09 17:13:11 | 2024-10-09 17:13:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define MAXN 6010
#define MAXE 2 * 100010
#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 tag) -> int
{
return ((x - 1) * m + (y - 1)) * 2 + tag;
};
S = trans(n, m, 1) + 1, T = S + 1, N = T;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if(a[i][j] == '.')
continue;
addline(trans(i, j, 0), trans(i, j, 1), 1, 0);
for (int dx = -2; dx <= 2; dx++)
{
for (int dy = -(2 - abs(dx)); dy <= 2 - abs(dx); dy++)
{
if(dy == 0 && dx == 0)
continue;
int x = i + dx, y = j + dy;
if(x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] == '#')
{
addline(trans(x, y, 1), trans(i, j, 0), INF, 0);
// cerr << x << "," << y << " -> " << i << "," << j << endl;
}
}
}
}
}
for (int i = 1; i <= n; i++)
{
addline(S, trans(i, 1, 0), INF, 0);
addline(trans(i, m, 1), T, INF, 0);
}
for (int j = 1; j <= m; j++)
{
addline(S, trans(n, j, 0), INF, 0);
addline(trans(1, j, 1), 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: 2ms
memory: 8364kb
input:
4 6 .##... .#.... ##.... ....#.
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 8332kb
input:
20 30 ...########################### #..########################### ...########################### ...########################### .############################# ...########################### ...########################### #..########################### ...########################### ..#...............
output:
7
result:
wrong answer 1st numbers differ - expected: '11', found: '7'