1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include<bits/stdc++.h> using namespace std;
typedef long long ll; typedef pair <int,int> Pii; #define pb push_back #define mp make_pair #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
char IO; int rd(){ int s=0; int f=0; while(!isdigit(IO=getchar())) if(IO=='-') f=1; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; }
const int N=25*30*3,M=N<<3,INF=1e9+10;
int n,m,k; int a[30][30];
int id[30][30][2]; struct Edge{ int to,nxt,w,c; }e[M]; int head[N],ecnt,S,T,V; void Clear(){ rep(i,1,V) head[i]=0; ecnt=1,V=0; } void AddEdge(int u,int v,int w,int c) { e[++ecnt]=(Edge){v,head[u],w,c}; head[u]=ecnt; } void Link(int u,int v,int w,int c=0){ AddEdge(u,v,w,c),AddEdge(v,u,0,-c); } #define erep(u,i) for(int i=head[u],v=e[i].to,w=e[i].w,c=e[i].c;i;i=e[i].nxt,v=e[i].to,w=e[i].w,c=e[i].c)
int dis[N]; int SPFA(){ static int inq[N]; static queue <int> que; rep(i,1,V) dis[i]=INF; que.push(S),dis[S]=0; while(!que.empty()) { int u=que.front(); que.pop(); inq[u]=0; erep(u,i) if(w && dis[v]>dis[u]+c) { dis[v]=dis[u]+c; if(!inq[v]) inq[v]=1,que.push(v); } } return dis[T]<INF; } int Dfs(int u,int in) { if(u==T) return in; int out=0,t=dis[u]; dis[u]=INF; erep(u,i) if(w && dis[v]==t+c) { int t=Dfs(v,min(in-out,w)); e[i].w-=t,e[i^1].w+=t,out+=t; if(in==out) break; } if(out) dis[u]=t; return out; }
Pii Dinic(){ int flow=0,cost=0; while(SPFA()) for(int t;(t=Dfs(S,INF));) flow+=t,cost+=dis[T]*t; return mp(flow,cost); }
class CurvyonRails { public: int getmin(vector <string> field) { n=field.size(),m=field[0].size(); rep(i,0,n-1) rep(j,0,m-1) a[i+1][j+1]=field[i][j]; k=0,Clear(); rep(i,1,n) rep(j,1,m) if(a[i][j]!='w') { k++; rep(d,0,1) id[i][j][d]=++V; } S=++V,T=++V; rep(i,1,n) rep(j,1,m) if(a[i][j]!='w') { if((i+j)&1){ Link(S,++V,2,0); rep(d,0,1) { Link(V,id[i][j][d],1,0); Link(V,id[i][j][d],1,a[i][j]=='C'); } if(i<n && a[i+1][j]!='w') Link(id[i][j][0],id[i+1][j][0],1,0); if(j<m && a[i][j+1]!='w') Link(id[i][j][1],id[i][j+1][1],1,0); } else { Link(++V,T,2,0); rep(d,0,1) { Link(id[i][j][d],V,1,0); Link(id[i][j][d],V,1,a[i][j]=='C'); } if(i<n && a[i+1][j]!='w') Link(id[i+1][j][0],id[i][j][0],1,0); if(j<m && a[i][j+1]!='w') Link(id[i][j+1][1],id[i][j][1],1,0); } } Pii ans=Dinic(); if(ans.first!=k) return -1; return ans.second; } };
|