[JZOJ 5046] Robot game

Description

Jing Jing Hall has a digital format, it is the disintegration of the king's favorite place to go.
Legend, this magnificent digital format, there are N rows and N columns, each of the grid has a number.
Dares to defy the king decides to challenge the disintegration of the Road by topic was ninety-nine percent.
The first row and first column of the grid are given:
F. [K,. 1] = L K
F. [. 1, K] T = K
For other lattice, recursive satisfied:
F. [I, J] = A F. [I, J-. 1] + B F. [. 1-I, J] + C
as expected, the disintegration can be obtained when the king F [n, n] (mod 1000003) , the pass rate will reach one hundred percent.

Input

Title comprising a plurality of sets of test data, the test data each representing 3 rows.
For each test:
The first line contains four integers N, a, b, c.
The second line contains N integers, represents l [1], ..., l [N]
The third line contains N integers, represents t [1], ..., t [N]

Output

For each test, an output line, comprising an integer, represents the answer to the disintegration of the king obtained.

Sample Input
3 0 0 0
0 0 2
0 3 0
4 3 5 2
7 1 4 3
7 4 4 8

Sample Output

0
41817

Data Constraint

10% of the data is: N <= 1000
30% of the data: N <= 10000
100% data: N <= 200000, a, b, c <= 1000000, l [1] = t [1], 1 <= l [k], t [k ] <= 1000000

Thinking

We can build on the information according to arrow board a directed graph, where each node of FIG out a maximum of 1, the
order we obtain a section showing a structure of the following two:

  1. Directed tree
  2. Cyclo plurality directed tree
    corresponds to a tree from a node to a corresponding node of each path from the first column to the last column is the node
    path roots. We can first column node as a special node, the remaining question becomes how the tree
    at some point were stained, so as to contain one and only one colored point on these specific points to the root path.
    In the process of implementation, in order to facilitate the achievement, we can add one more secondary roots, the roots are all connected to the secondary root
    a directed edge. This would be a problem on multiple trees into question a tree.
    The remaining problems can be solved using dynamic programming. K represents the i-th point before we tree to be stained with F x (x, i, k)
    color, so that whether the subtree each particular path point node x has one and only one colored dot. Suppose we have the i-
    th tree transfected with k nodes, if f (x, i + 1, ky) and f (child [x] [i ], 0, y) value are 1, then f (x, values i, k) may be
    1. If there is a ring, it must not last a point on the ring can imagine, so the point on the ring can be dyed any. This
    kind of way, the overall time complexity is O (NMK ^ 2) a.
    To speed up the calculation process, we can state compressed by binary strings, all of f (x, i + 1, k) compressed into a
    k-bit binary string represented as F [x] [i]. In addition, we can represent F [x] [i] a value obtained by reversing R [x] [i]. We can push
    derive F [x] [i] = (R [x] [i] >> (50-k)) & F [child [x] [i]]. This way, the time complexity is optimized O (NMK).

Code

#include<bits/stdc++.h>
using namespace std;
const int N=1000077,K=51;
int n,m,i,j,k,tot,ans,s,t,x,y,z,r,c,last,ss;
int w[5],go[N],g[N][K];
int q[N][2],nex[N],head[N],dfs[N],dfn[N],size[N];
bool f[N][K];
char ch;
bool bz[N],b[N];
bool dg(int i,int fa) {
	bool re=false;
	dfn[i]=++t; size[dfn[i]]=1;
	int last;
	dfs[t]=i;
	if (bz[dfn[fa]]||fa%c==1) bz[dfn[i]]=true;
	int j=head[i];
	while (j>0) {
		last=t;
		if (dg(q[j][1],i)) {
			size[dfn[i]]+=size[dfn[q[j][1]]];
			re=true;
		}
		else {
			for (int k=last+1;k<=t;k++) bz[k]=false;
			t=last;
		}
		j=nex[j];
	}
	
	if (i%c==1) re=true;
	return re;
}
int main() {
	freopen("robots.in","r",stdin);
	freopen("robots.out","w",stdout);
	
	scanf("%d %d %d\n",&r,&c,&k);
	w[1]=-c; w[2]=c; w[3]=-1; w[4]=1; 
	
	tot=0;
	for (i=1;i<=r;i++) {
		for (j=1;j<=c;j++) {
			++tot;
			ch=getchar();
			if (j==c) go[tot]=0;
			else {
				if (ch=='U') go[tot]=tot+w[1];
				else if (ch=='D') go[tot]=tot+w[2];
				else if (ch=='L') go[tot]=tot+w[3];
				else if (ch=='R') go[tot]=tot+w[4];
			}
			
			q[tot][0]=go[tot]; q[tot][1]=tot;
		}
		scanf("\n");
	}
	
	for (i=1;i<=tot;i++) {
		nex[i]=head[q[i][0]];
		head[q[i][0]]=i;
	}
	
	j=head[0]; t=-1; dfn[0]=0; size[0]=1;
	dg(0,0);
	while (j>0) {
		bz[dfn[q[j][1]]]=true;
		j=nex[j];
	}
	
	f[1][0]=true;
	for (i=1;i<=t;i++) {
		for (j=0;j<=k;j++) {
			if (!f[i][j]) continue;
			
			if (j+1<=k&&!bz[i]) {
				f[i+size[i]][j+1]=true;
				g[i+size[i]][j+1]=i;
			}
			if (size[i]!=1) {
				f[i+1][j]=true;
				g[i+1][j]=g[i][j];
			}
		}
	}
	
	for (i=1;i<=t;i++) b[dfs[i]]=true;
	for (i=1;i<=r;i++) {
		if (!b[i*c]) {
			b[i*c]=true;
			ss++;
		}
	}
	
	for (i=k;i>=0;i--) {
		if (f[t+1][i]&&k-i<=r*c-t-ss) {
			x=t+1; y=i;
			while (g[x][y]!=0) {
				z=dfs[g[x][y]];
				printf("%d %d\n",(z-1)/c+1,(z-1)%c+1);
				x=g[x][y]; y--;
			}
			s=k-i;
			for (j=1;j<=r*c;j++) {
				if (s==0) break; 
				if (!b[j]) {
					printf("%d %d\n",(j-1)/c+1,(j-1)%c+1);
					s--;
				}
			}
			return 0;
		}
	}
	
	printf("-1\n");
	return 0;
}
发布了703 篇原创文章 · 获赞 392 · 访问量 14万+

Guess you like

Origin blog.csdn.net/Eric1561759334/article/details/104072528