QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416526 | #2563. Curly Racetrack | Kevin5307 | RE | 2ms | 8620kb | C++23 | 4.5kb | 2024-05-21 22:13:37 | 2024-05-21 22:13:39 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define int ll
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
class MinCostMaxFlow
{
private:
struct edge
{
int u,v,capa,cost;
edge(int _u,int _v,int _capa,int _cost)
{
u=_u;
v=_v;
capa=_capa;
cost=_cost;
}
edge(){}
}E[5005000];
vector<int> G[101000];
int p;
int dist[101000];
bool inque[101000];
bool vis[101000];
int head[100100];
bool spfa(int s,int t)
{
memset(dist,inf,sizeof(dist));
memset(inque,0,sizeof(inque));
dist[s]=0;
inque[s]=1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
inque[x]=0;
for(auto e:G[x])
if(E[e].capa&&dist[x]+E[e].cost<dist[E[e].v])
{
dist[E[e].v]=dist[x]+E[e].cost;
if(!inque[E[e].v])
{
q.push(E[e].v);
inque[E[e].v]=1;
}
}
}
return dist[t]<0;
}
int cost;
int dfs(int u,int t,int flow)
{
if(u==t)
return flow;
vis[u]=1;
int ans=0;
for(;head[u]<sz(G[u]);head[u]++)
{
int e=G[u][head[u]];
if(!vis[E[e].v]&&E[e].capa&&dist[E[e].v]==dist[u]+E[e].cost)
{
int augflow=dfs(E[e].v,t,min(E[e].capa,flow-ans));
if(augflow)
{
cost+=augflow*E[e].cost;
E[e].capa-=augflow;
E[e^1].capa+=augflow;
ans+=augflow;
}
}
}
vis[u]=0;
return ans;
}
public:
void clear()
{
p=0;
for(int i=0;i<101000;i++)
G[i].clear();
memset(vis,0,sizeof(vis));
}
MinCostMaxFlow()
{
clear();
}
void addEdge(int u,int v,int capa,int cost)
{
G[u].pb(p);
E[p++]=edge(u,v,capa,cost);
G[v].pb(p);
E[p++]=edge(v,u,0,-cost);
}
pii mcmf(int s,int t)
{
cost=0;
int ans=0;
while(spfa(s,t))
{
memset(head,0,sizeof(head));
int x;
while((x=dfs(s,t,inf))) ans+=x;
}
return mp(ans,cost);
}
}mcmf;
int n,m;
char grid[105][105];
int nd1[105][105],nd2[105][105];
int tot1,tot2;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",grid[i]+1);
for(int i=1;i<=n;i++)
{
vector<int> vp;
vp.pb(0);
for(int j=1;j<=m;j++)
if(isdigit(grid[i][j]))
vp.pb(j);
vp.pb(m+1);
for(int j=1;j<sz(vp);j++)
{
int st1=(vp[j-1]?(grid[i][vp[j-1]]>'2'):0);
int st2=(vp[j]<=m?(grid[i][vp[j]]>'2'):1);
bool flag=1;
for(int k=vp[j-1]+1;k<vp[j];k++)
if(grid[i][k]=='x')
flag=0;
if((st1^st2^vp[j-1]^vp[j])&1) if(flag)
{
tot1++;
for(int k=vp[j-1]+1;k<vp[j];k++)
nd1[i][k]=tot1;
}
}
}
for(int i=1;i<=m;i++)
{
vector<int> vp;
vp.pb(0);
for(int j=1;j<=n;j++)
if(isdigit(grid[j][i]))
vp.pb(j);
vp.pb(n+1);
for(int j=1;j<sz(vp);j++)
{
int st1=(vp[j-1]?((grid[vp[j-1]][i]&1)^1):0);
int st2=(vp[j]<=m?((grid[vp[j]][i]&1)^1):1);
bool flag=1;
for(int k=vp[j-1]+1;k<vp[j];k++)
if(grid[k][i]=='x')
flag=0;
if((st1^st2^vp[j-1]^vp[j])&1) if(flag)
{
bool flag2=0;
tot2++;
for(int k=vp[j-1]+1;k<vp[j];k++)
nd2[k][i]=tot2,
flag2|=(grid[k][i]=='.');
if(n==100)
assert(flag2);
}
}
}
for(int i=1;i<=tot1;i++)
{
mcmf.addEdge(0,i,1,-inf);
mcmf.addEdge(0,i,inf,0);
}
for(int i=1;i<=tot2;i++)
{
mcmf.addEdge(i+tot1,tot1+tot2+1,1,-inf);
mcmf.addEdge(i+tot1,tot1+tot2+1,inf,0);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) if(grid[i][j]=='.')
{
if(nd1[i][j]&&nd2[i][j])
mcmf.addEdge(nd1[i][j],tot1+nd2[i][j],1,1);
else if(nd1[i][j])
mcmf.addEdge(nd1[i][j],tot1+tot2+1,1,1);
else if(nd2[i][j])
mcmf.addEdge(0,tot1+nd2[i][j],1,1);
}
ll val=mcmf.mcmf(0,tot1+tot2+1).second;
val+=1ll*(tot1+tot2)*inf;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(grid[i][j]=='x')
val++;
if(val<0||val>n*m)
cout<<"-1\n";
else
cout<<n*m-val<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 8620kb
input:
4 5 4...x .2..2 .o1x. 3..3o
output:
12
result:
ok 1 number(s): "12"
Test #2:
score: 0
Accepted
time: 2ms
memory: 8484kb
input:
2 3 4o2 3x1
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: -100
Runtime Error
input:
100 47 .....4.42..4.2....4242........o242424....o4.... 3...3........31...1......2..3...o1313.......13. .o..........2...14...........2..4........2..2.. ..................2....232.......4..13..x.41... 42...1.4....3..3...1.2.32...1...4...424..2...4. .1.3.2...242...4...4..............23........... ..x.....