QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#206387 | #5114. Cells Coloring | KULIANLEN | WA | 391ms | 30936kb | C++20 | 3.5kb | 2023-10-07 20:15:25 | 2023-10-07 20:15:27 |
Judging History
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;
for(int j = n*2+m*2-1;j<=cnt;j++)
{
if(j&1)w[j] = 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);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 24788kb
input:
3 4 2 1 .*** *..* **..
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 2ms
memory: 26712kb
input:
3 4 1 2 .*** *..* **..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 116ms
memory: 28760kb
input:
250 250 965680874 9042302 ..**.*****..**..**.*****..***..***.**......*.***.*...***.*....*.**.*.**.*.*.****...*.******.***.************....**.*..*..***.*******.*.***.*..**..****.**.*.*..***.****..**.....***.....*.****...*...*.***..****..**.*.*******..*.*******.*.*.*.****.*.*** ....**.*******.*.******...
output:
109972100048
result:
ok 1 number(s): "109972100048"
Test #4:
score: 0
Accepted
time: 152ms
memory: 26752kb
input:
250 250 62722280 506434 *.**.***.*.*....*....*...**.*..**..****.*.*..*.*.*..*.....**..*.*.*.*****.*.**..*.**....***..*..*.*.*.**.*..*..*.**..*...**....**..*.*.***.*****.*****.***..**.***.****....*.*.**.**.*...****....*..*.**.**********.......********...***.**..*.....**.*..* .*..********..*...*..****...
output:
8437726048
result:
ok 1 number(s): "8437726048"
Test #5:
score: 0
Accepted
time: 391ms
memory: 26664kb
input:
250 250 85956327 344333 ..*.............*...*.....*...*..........*.........*...*.......*..***......*.*........*.*........*........*..*..*.............*.*........*....*..*................***...................*..*.............*..*.....*..**..............*..*......*.....*..** .........*......*.*.........
output:
18268031127
result:
ok 1 number(s): "18268031127"
Test #6:
score: 0
Accepted
time: 0ms
memory: 30936kb
input:
250 250 768323813 489146 ...*................*...........*.................*..***..*.......*..*......*.................*...*.........*.*.*.*...*.*.*.*.*.......*........*.............*...............*..*.............*.*...*.....................**.....**.....*.*........*...... ...................*.......
output:
25999088192
result:
ok 1 number(s): "25999088192"
Test #7:
score: -100
Wrong Answer
time: 103ms
memory: 24696kb
input:
250 250 865365220 7248935 .....**.*.***...**.**...*.**.*****..****.**.**.*...*..**....*.**.*..**..*..*.****....***.***.*...*.*.*.**..****.***.*.**..*****.**..*.*.***..***.*..*.*..*......*.*******.*******.*..*.******.....**.***...*****...*...**....**.**.*...*...**.*.*****...*. *..*.**.*...****.*.**.*...
output:
97494358600
result:
wrong answer 1st numbers differ - expected: '97440874100', found: '97494358600'