QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#782731#5114. Cells Coloringliuyz11WA 116ms12032kbC++142.8kb2024-11-25 21:10:572024-11-25 21:10:57

Judging History

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

  • [2024-11-25 21:10:57]
  • 评测
  • 测评结果:WA
  • 用时:116ms
  • 内存:12032kb
  • [2024-11-25 21:10:57]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(x, l, r) for(int x = l; x <= r; x++)
#define int long long 
using namespace std;

const int INF = 1e14;
const int MAXN = 1e4 + 5;
const int MAXM = 3e6 + 5;
queue<int> que;
int S, T, cnt, mincost;
int head[MAXN], cur[MAXN], nxt[MAXM], to[MAXM], value[MAXM], cost[MAXM];
int dis[MAXN], s[MAXN], E[MAXN];
char st[MAXN];
bool vis[MAXN];


void init(){
    cnt = 0;
    rep(i, S, T) head[i] = -1;
}

void add(int u, int v, int w, int c){
    nxt[cnt] = head[u];
    head[u] = cnt;
    to[cnt] = v;
    value[cnt] = w;
    cost[cnt] = c;
    cnt++;
}

void addedge(int u, int v, int w, int c){
    add(u, v, w, c);
    add(v, u, 0, -c);
}

bool spfa(){
    while(!que.empty()) que.pop();
    rep(i, S, T) dis[i] = INF;
    que.push(S);
    dis[S] = 0;
    vis[S] = 1;
    while(!que.empty()){
        int u = que.front();
        que.pop();
        vis[u] = 0;
        for(int e = head[u]; e != -1; e = nxt[e]){
            int v = to[e];
            if(!value[e] || dis[v] <= dis[u] + cost[e]) continue;
            dis[v] = dis[u] + cost[e];
            if(!vis[v]){
                que.push(v);
                vis[v] = 1;
            }
        }
    }
    return dis[T] != INF;
}

int dfs(int u, int flow){
    if(u == T) return flow;
    vis[u] = 1;
    int res = 0;
    for(int &e = cur[u]; e != -1; e = nxt[e]){
        int v = to[e];
        if(vis[v] || !value[e] || dis[v] != dis[u] + cost[e]) continue;
        int q = dfs(v, min(flow, value[e]));
        mincost += q * cost[e];
        value[e] -= q;
        value[e ^ 1] += q;
        res += q;
        flow -= q;
        if(!flow) break;
    }
    if(!res) dis[u] = INF;
    vis[u] = 0;
    return res;
}

int dinic(){
    int ans = 0; mincost = 0;
    while(spfa()){
        rep(i, S, T) cur[i] = head[i];
        ans += dfs(S, INF);
    }
    return ans;
}

signed main(){
    int n, m, c, d;
    scanf("%lld%lld%lld%lld", &n, &m, &c, &d);
    S = 0, T = n + m + 1;
    init();
    rep(i, 1, n){
        scanf("%s", st);
        rep(j, 1, m){
            if(st[j - 1] == '.'){
                addedge(i, n + j, 1, d);
                s[i]++;
                s[n + j]++;
            }
        }
    }
    int mx = 0;
    rep(i, 1, n){
        mx = max(mx, s[i]);
        E[i] = cnt;
        addedge(S, i, 0, 0);
    }
    rep(i, 1, m){
        mx = max(mx, s[n + i]);
        E[n + i] = cnt;
        addedge(n + i, T, 0, 0);
    }
    int ans = c * mx, res = 0;
    // printf("%d\n", ans);
    while(mx--){
        rep(i, 1, n + m){
            value[E[i]]++;
        }
        int xx = dinic();
        // printf("%lld : %lld %lld %lld\n", mx, xx, res, mincost);
        res += mincost;
        ans = min(ans, mx * c + res);
    }
    printf("%lld\n", ans);
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 12032kb

input:

3 4 2 1
.***
*..*
**..

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 9924kb

input:

3 4 1 2
.***
*..*
**..

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 116ms
memory: 10752kb

input:

250 250 965680874 9042302
..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.***
....**.*******.*.******...

output:

122641470998

result:

wrong answer 1st numbers differ - expected: '109972100048', found: '122641470998'