QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#328944 | #1197. Draw in Straight Lines | hjl666 | TL | 0ms | 0kb | C++14 | 3.7kb | 2024-02-16 10:46:08 | 2024-02-16 10:46:08 |
answer
#include<bits/stdc++.h>
#define int ll
#define ll long long
#define db double
#define MP make_pair
#define vec vector<int>
#define pii pair<int,int>
#define pb emplace_back
#define fi first
#define se second
#define ls k<<1
#define rs k<<1|1
#define CLK (double)clock()/CLOCKS_PER_SEC
using namespace std;
mt19937 rnd(time(0));
int read(){
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
void write(int x){
if(x<0){putchar('-');write(-x);}
else {
if(x>9)write(x/10);
putchar(x%10+'0');
}
}
const int N=1e4+5,M=5e5+5,L=45,inf=1e18;
int n,m,a,b,c,bh[L][L],bv[L][L],wh[L][L],wv[L][L];
namespace MF{
int S,T,tot,maxflow,l[N],cur[N],vis[N];
int cnt=1,to[M],w[M],nxt[M],first[N];
void add(int x,int y,int wgh){
to[++cnt]=y,w[cnt]=wgh,nxt[cnt]=first[x],first[x]=cnt;
to[++cnt]=x,w[cnt]=0,nxt[cnt]=first[y],first[y]=cnt;
}
bool bfs(){
for(int i=1;i<=tot;i++)l[i]=0,cur[i]=first[i];
queue<int> q;q.push(S),l[S]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=first[x];i;i=nxt[i]){
int y=to[i];
if(w[i]&&!l[y])l[y]=l[x]+1,q.push(y);
}
}
return l[T]>0;
}
int dinic(int x,int flow){
if(x==T||!flow)return maxflow+=flow,flow;
int rest=flow;
for(int i=cur[x];i&&rest;i=nxt[i]){
int y=to[i];cur[x]=i;
if(w[i]&&l[y]==l[x]+1){
int in=dinic(y,min(rest,w[i]));
w[i]-=in,w[i^1]+=in;
rest-=in;
}
}
return flow-rest;
}
int solve(){
while(bfs())dinic(S,inf);
return maxflow;
}
void clear(){
for(int i=1;i<=tot;i++)first[i]=vis[i]=0;
maxflow=tot=0,cnt=1;
}
}using namespace MF;
void dfs(int x){
if(vis[x])return ;vis[x]=1;
for(int i=first[x];i;i=nxt[i])if(w[i])dfs(to[i]);
}
void paint(){
dfs(S);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)cout<<vis[bh[i][j]];
cout<<"\n";
}
}
void MAIN(){
n=read(),m=read(),a=read(),b=read(),c=read();
S=++tot,T=++tot;
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
bh[i][j]=++tot;//h with black->cut S
bv[i][j]=++tot;//v without black->cut T
wh[i][j]=++tot;//h without white->cut T
wv[i][j]=++tot;//v with white->cut S
add(S,bh[i][j],a);
add(bv[i][j],T,a);
add(wh[i][j],T,a);
add(S,wv[i][j],a);
if(i>1){
add(bh[i][j],bh[i-1][j],b);
add(wh[i-1][j],wh[i][j],b);
}
if(i==n){
add(S,bh[i][j],b);
add(wh[i][j],T,b);
}
if(j>1){
add(bv[i][j-1],bv[i][j],b);
add(wv[i][j],wv[i][j-1],b);
}
if(j==m){
add(bv[i][j],T,b);
add(S,wv[i][j],b);
}
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
int x;scanf("%1d",&x);
if(x==1){
add(bh[i][j],bv[i][j],c);
add(wh[i][j],T,inf);
add(S,wv[i][j],inf);
}
else {
add(wv[i][j],bh[i][j],c);
add(bv[i][j],wh[i][j],c);
add(bv[i][j],bh[i][j],inf);
}
}
cout<<solve()<<"\n";
// paint();
clear();
}
signed main(){
// freopen("b.in","r",stdin);
// freopen("b.out","w",stdout);
int T=read();while(T--)MAIN();
// printf("\nTIME:%lf\n",CLK);
return 0;
}
// g++ b.cpp -o b -std=c++14 -O2
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 3 1 2 3 .#. ### .#.