QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#624778#9224. Express Evictionrikka_lylyWA 0ms8368kbC++203.6kb2024-10-09 16:36:372024-10-09 16:36:40

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 16:36:40]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:8368kb
  • [2024-10-09 16:36:37]
  • 提交

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);
            if(j != 1 && a[i][j - 1] == '#')
            {
                addline(trans(i, j - 1, 1), trans(i, j, 0), INF, 0);
            }
            if(i != n && a[i + 1][j] == '#')
            {
                addline(trans(i + 1, j, 1), trans(i, j, 0), INF, 0);
            }
            if(j != 1 && i != n && a[i + 1][j - 1] == '#')
            {
                addline(trans(i + 1, j - 1, 1), trans(i, j, 0), INF, 0);
            }
        }
    }
    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: 0ms
memory: 8352kb

input:

4 6
.##...
.#....
##....
....#.

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 8368kb

input:

20 30
...###########################
#..###########################
...###########################
...###########################
.#############################
...###########################
...###########################
#..###########################
...###########################
..#...............

output:

7

result:

wrong answer 1st numbers differ - expected: '11', found: '7'