2150: Tribal Wars
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://www.lydsy.com/JudgeOnline/problem.php?id=2150
Description
The tribes of Lanzerb in the upper part of country A, they were dissatisfied with the freezing environment, so they were prepared to go to the lower part of country A to gain greater territory. Country A is a m*n matrix, some of which are towns, some of which are Alpine Shen Jian uninhabited. Lanzerb his tribe into several armies, they agreed: 1. Each army can start from any town and can only go from the top to the bottom and not back. On the way only through town, not through the Alpine Shen Jian. 2. If a town is visited by an army, no other army can go to that town. 3. Every army can stop fighting in any town. 4. All the troops were strange, and they went a bit like a horse in chess. But the horse can only walk 1*2 route, and they can only go r*c route. Lanzerb's ambition made his goal to unify the country, but the force limit made them powerless in staffing. Assuming that each of them can successfully occupy all the towns that the army passes through, please help lanzerb calculate at least how many troops are needed to accomplish the reunification of the whole nation.
Input
The first line contains 4 integers m, N, R, C, meaning the description of the problem. The next M row is a string of length n for each row. If a character is '. ', it means that the place is a town; if the character is ' X ', it means the place is Alpine Shen Jian.
Output
Outputs an integer that represents the minimum number of armies.
Sample Input
3 3 1 2
...
. x.
...
Sample Output
4HINT
Test instructions
Exercises
Minimum path coverage problem
It's better to subtract the maximum flow by the total.
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<bitset>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200500#defineMoD 1001#defineEPS 1e-9#definePi 3.1415926intNum;//const int INF=0X7FFFFFFF;Constll inf=999999999; inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//*************************************************************************************namespacenetflow{Const intmaxn=100000, maxm=100000, inf=1e9; structEdge {intV,c,f,nx; Edge () {} Edge (intVintCintFintNX): V (v), C (c), F (f), NX (NX) {}} E[MAXM]; intG[maxn],cur[maxn],pre[maxn],dis[maxn],gap[maxn],n,sz; voidInitint_n) {N=_n,sz=0; memset (g,-1,sizeof(g[0])*N); } voidLinkintUintVintc) {E[sz]=edge (V,c,0, G[u]); g[u]=sz++; E[SZ]=edge (U,0,0, G[v]); g[v]=sz++; } intISAP (intSintT) {//T-S intmaxflow=0, aug=inf,flag=false, U,v; for(intI=0; i<n;++i) cur[i]=g[i],gap[i]=dis[i]=0; for(gap[s]=n,u=pre[s]=s;dis[s]<n;flag=false) { for(int&it=cur[u];~it;it=e[it].nx) { if(e[it].c>e[it].f&&dis[u]==dis[v=e[it].v]+1) { if(AUG>E[IT].C-E[IT].F) aug=e[it].c-e[it].f; PRE[V]=u,u=v; flag=true; if(u==T) { for(maxflow+=aug;u!=S;) {E[cur[u=pre[u]]].f+=; E[cur[u]^1].f-=; } the=inf; } Break; } } if(flag)Continue; intmx=N; for(intit=g[u];~it;it=e[it].nx) { if(e[it].c>e[it].f&&dis[e[it].v]<mx) {mx=DIS[E[IT].V]; cur[u]=it; } } if((--gap[dis[u]) = =0) Break; ++gap[dis[u]=mx+1]; u=Pre[u]; } returnMaxflow; } BOOLBFsintSintT) {Static intQ[MAXN]; memset (dis,-1,sizeof(dis[0])*N); Dis[s]=0; q[0]=R; for(intH=0, t=1, u,v,it;h<t;++h) { for(u=q[h],it=g[u];~it;it=e[it].nx) { if(dis[v=e[it].v]==-1&&E[it].c>e[it].f) {Dis[v]=dis[u]+1; q[t++]=v; } } } returndis[t]!=-1; } intDfsintUintTintLow ) { if(u==t)returnLow ; intret=0, Tmp,v; for(int&it=cur[u];~it&&ret<low;it=e[it].nx) { if(dis[v=e[it].v]==dis[u]+1&&E[it].c>e[it].f) { if(Tmp=dfs (V,t,min (low-ret,e[it].c-E[IT].F))) {ret+=tmp; e[it].f+=tmp; e[it^1].f-=tmp; } } } if(!ret) dis[u]=-1;returnret; } intDinic (intSintT) {intmaxflow=0, TMP; while(BFS (s,t)) {memcpy (cur,g,sizeof(g[0])*N); while(Tmp=dfs (S,t,inf)) maxflow+=tmp; } returnMaxflow; }}using namespaceNetFlow;strings[ -];intm[ the][ the][3];inttot=1;intGET_ID (intXintYintz) { if(M[x][y][z])returnM[x][y][z]; Else returnm[x][y][z]=tot++;}intn,m,r,c;intJudgeintXinty) { if(x>=n| | x<0)return 0; if(y>=m| | y<0)return 0; if(s[x][y]=='.') return 1; return 0;}intMain () {init (100000); N=read (), M=read (), C=read (), r=read (); for(intI=0; i<n;i++) Cin>>S[i]; intAns =0; for(intI=0; i<n;i++) { for(intj=0; j<m;j++) { if(s[i][j]=='.') {ans++; Link (get_id (0,0,0), get_id (I,j,1),1); Link (get_id (i,j,2), get_id (1,0,0),1); intx, y; X=i+r;y=j+C; if(Judge (x, Y)) Link (get_id (i,j,1), get_id (x, Y,2),1); X=i+r;y=j-C; if(Judge (x, Y)) Link (get_id (i,j,1), get_id (x, Y,2),1); X=i+c;y=j+R; if(Judge (x, Y)) Link (get_id (i,j,1), get_id (x, Y,2),1); X=i+c;y=j-R; if(Judge (x, Y)) Link (get_id (i,j,1), get_id (x, Y,2),1); } } } intK = Dinic (get_id (0,0,0), get_id (1,0,0)); printf ("%d\n", ans-K);}
Bzoj 2150: The largest stream of tribal wars