QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#206397#5114. Cells ColoringKULIANLENWA 384ms28908kbC++203.4kb2023-10-07 20:18:252023-10-07 20:18:26

Judging History

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

  • [2023-10-07 20:18:26]
  • 评测
  • 测评结果:WA
  • 用时:384ms
  • 内存:28908kb
  • [2023-10-07 20:18:25]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int ni=4e5;
int h[ni],e[ni],ne[ni],fr[ni],f[ni],idx,w[ni];
int cur[ni];
const int inf=2147483647;//inf:最大值 


void add(int a,int b,int c){
    e[idx]=b;w[idx]=c;
    ne[idx]=h[a];fr[idx]=a;
    h[a]=idx++;
}

long long n,m,c,d,ti,si,cnt;
long long ans=1e18;
int di[ni];        
int dep[ni],gap[ni];//dep[i]表示节点i的深度,gap[i]表示深度为i的点的数量 
void bfs()//倒着搜 
{
    memset(dep,-1,sizeof(dep));//把深度变为-1(0会导致gap崩坏) 
    memset(gap,0,sizeof(gap));
    dep[ti]=0;//汇点深度为0 
    gap[0]=1;//深度为0的点有1个 
    queue<int>q; 
    q.push(ti);//t点入栈 
    while(!q.empty())
	{
        int u=q.front();
        q.pop();
        for(int i = h[u];~i;i=ne[i])//head[u]:u点所在的边,node[i].next:u点所在的边的下一个点,就这样遍历下去 
		{
            int v=e[i];//v为当前边的下一个点 
            if(dep[v]!=-1)continue;//dep[v]!=-1相当于v点已被遍历||不管 
            q.push(v);
            dep[v]=dep[u]+1;//v点的深度比u点大1 
            gap[dep[v]]++;
        }//直到所有点都被遍历过 
    }
    return;
}//从t到s跑一遍bfs,标记深度
long long maxflow;

int dfs(int u,int flow)
{
    if(u==ti)
	{
        maxflow+=flow;
        return flow;
    }
    int used=0;
    for(int i = h[u];~i;i=ne[i])//head[u]:u点所在的边,node[i].next:u点所在的边的下一个点,就这样遍历下去 
	{
        // cout << e[i] << endl;
        int d=e[i];
        if(w[i]&&dep[d]+1==dep[u])//如果这条边的残量大于0,且没有断层 
		{
            int mi=dfs(d,min(w[i],flow-used));
            if(mi){
                w[i]-=mi;
                w[i^1]+=mi;
                used+=mi;
            }
            if(used==flow)return used;
        }
    }
    //如果已经到了这里,说明该点出去的所有点都已经流过了
    //并且从前面点传过来的流量还有剩余
    //则此时,要对该点更改dep
    //使得该点与该点出去的点分隔开
    --gap[dep[u]];
    if(gap[dep[u]]==0)dep[si]=n+m+3;//出现断层,无法到达t了
    dep[u]++;//层++ 
    gap[dep[u]]++;//层数对应个数++
    return used; 
}
long long ISAP()
{
    maxflow=0;
    bfs();
    while(dep[si]<n+m+2)dfs(si,inf);//每走一遍增广路,s的层数会加1,如果一直没有出现断层,最多跑n-dep(刚bfs完时s的深度)条增广路共有n个点 
    return maxflow;
}
signed main(){
    memset(h,-1,sizeof(h));
    scanf("%lld%lld%lld%lld",&n,&m,&c,&d);
    si=0;ti=n+m+1;
    for(int i=1;i<=n;i++)
    add(0,i,1),add(i,0,0);
    for(int i=1;i<=m;i++)
    add(n+i,ti,1),add(ti,n+i,0);
    for(int i=1;i<=n;i++){
        string s;cin>>s;
        for(int j=0;j<m;j++)
        if(s[j]=='.'){
            add(i,j+1+n,1);
            add(j+1+n,i,0);cnt++;
        }
    }

    ans=cnt*d;
    // printf("(%d)",cnt);
    for(int i=1;i<=max(n,m);i++){
        // printf("_____________________\n");
        // for(int i=0;i<idx;i++){
        //     printf("%d %d %d %d\n",i,fr[i],e[i],f[i]);
        // }
        for(int j=0;j<n+m;j++)
        w[j<<1]=1,w[j<<1|1]=0;
       
        int k=ISAP();
        cnt-=k;
        // printf("%d %d\n",i,cnt);
        ans=min(ans,c*i+d*cnt);
        if(k*d<=c)break;
    }
    printf("%lld\n",ans);
}

详细

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 26932kb

input:

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

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 108ms
memory: 26960kb

input:

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

output:

109972100048

result:

ok 1 number(s): "109972100048"

Test #4:

score: 0
Accepted
time: 151ms
memory: 26900kb

input:

250 250 62722280 506434
*.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..*
.*..********..*...*..****...

output:

8437726048

result:

ok 1 number(s): "8437726048"

Test #5:

score: 0
Accepted
time: 384ms
memory: 28908kb

input:

250 250 85956327 344333
..*.............*...*.....*...*..........*.........*...*.......*..***......*.*........*.*........*........*..*..*.............*.*........*....*..*................***...................*..*.............*..*.....*..**..............*..*......*.....*..**
.........*......*.*.........

output:

18268031127

result:

ok 1 number(s): "18268031127"

Test #6:

score: 0
Accepted
time: 7ms
memory: 26880kb

input:

250 250 768323813 489146
...*................*...........*.................*..***..*.......*..*......*.................*...*.........*.*.*.*...*.*.*.*.*.......*........*.............*...............*..*.............*.*...*.....................**.....**.....*.*........*......
...................*.......

output:

25999088192

result:

ok 1 number(s): "25999088192"

Test #7:

score: -100
Wrong Answer
time: 103ms
memory: 27188kb

input:

250 250 865365220 7248935
.....**.*.***...**.**...*.**.*****..****.**.**.*...*..**....*.**.*..**..*..*.****....***.***.*...*.*.*.**..****.***.*.**..*****.**..*.*.***..***.*..*.*..*......*.*******.*******.*..*.******.....**.***...*****...*...**....**.**.*...*...**.*.*****...*.
*..*.**.*...****.*.**.*...

output:

97479860730

result:

wrong answer 1st numbers differ - expected: '97440874100', found: '97479860730'