CODE FESTIVAL 2016 Final

Submission #4267969

Source codeソースコード

#include <bits/stdc++.h>
using namespace std;
const int MAXN=3e2+7;
const int inf=1e9+7;
struct po {
	int from,to,w,nxt;
} edge[MAXN*MAXN*15];
int head[MAXN*MAXN+MAXN*6],num=-1,n,m,s,t,b[MAXN],dep[MAXN*MAXN+MAXN*6],cur[MAXN*MAXN+MAXN*6];
int U[MAXN],L[MAXN],R[MAXN],D[MAXN],id[MAXN][MAXN],I;
int us[MAXN][MAXN],vis[MAXN][MAXN];
char a[MAXN][MAXN];
inline void add_edge(int from,int to,int w) {
	edge[++num].nxt=head[from];
	edge[num].to=to;
	edge[num].w=w;
	head[from]=num;
}
inline void add(int from,int to,int w) {
	add_edge(from,to,w);
	add_edge(to,from,0);
}
inline bool bfs() {
	queue<int> q;
	memset(dep,0,sizeof(dep));
	while(!q.empty()) q.pop();
	q.push(s);
	dep[s]=1;
	while(!q.empty()) {
		int u=q.front();
		q.pop();
		for(int i=head[u]; i!=-1; i=edge[i].nxt) {
			int v=edge[i].to;
			if(edge[i].w&&!dep[v]) {
				q.push(v);
				dep[v]=dep[u]+1;
				if(v==t) return 1;
			}
		}
	}
	return 0;
}
inline int dfs(int u,int low) {
	if(u==t) return low;
	int diss=0;
	for(int &i=cur[u]; i!=-1; i=edge[i].nxt) {
		int v=edge[i].to;
		if(edge[i].w&&dep[v]==dep[u]+1) {
			int check=dfs(v,min(edge[i].w,low));
			if(check) {
				low-=check;
				diss+=check;
				edge[i].w-=check;
				edge[i^1].w+=check;
				if(low==0) break;
			}
		}
	}
	return diss;
}
inline int dinic() {
	int ans=0;
	while(bfs()) {
		for(int i=0; i<=t; i++) cur[i]=head[i];
		while(int d=dfs(s,inf)) ans+=d;
	}
	return ans;
}
int dfs1(int x,int y) {
	if(us[x][y]) {
		us[x][y]=0;
		return 1;
	}
	us[x][y]=1;
	if(a[x][y]=='L') {
		for(int i=1; i<y; i++) {
			if(!vis[x][i]&&dfs1(x,i)) {
				a[x][i]='L';
				if(us[x][y]) {
					us[x][y]=0;
					return 1;
				} else return dfs1(x,y);
			}
		}
	} else if(a[x][y]=='R') {
		for(int i=y+1; i<=n; i++) {
			if(!vis[x][i]&&dfs1(x,i)) {
				a[x][i]='R';
				if(us[x][y]) {
					us[x][y]=0;
					return 1;
				}else return dfs1(x,y);
			}
		}
	} else if(a[x][y]=='U') {
		for(int i=1; i<x; i++) {
			if(!vis[i][y]&&dfs1(i,y)) {
				a[i][y]='U';
				if(us[x][y]) {
					us[x][y]=0;
					return 1;
				}else return dfs1(x,y);
			}
		}
	} else {
		for(int i=x+1; i<=n; i++) {
			if(!vis[i][y]&&dfs1(i,y)) {
				a[i][y]='D';
				if(us[x][y]) {
					us[x][y]=0;
					return 1;
				}else return dfs1(x,y);
			}
		}
	}
	vis[x][y]=1;
	cout<<a[x][y];
	if(a[x][y]=='L'||a[x][y]=='R') cout<<x<<endl;
	else cout<<y<<endl;
	return 0;
}
int main() {
	memset(head,-1,sizeof(head));
	cin>>n;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++) id[i][j]=++I;
	for(int i=1; i<=n; i++) cin>>U[i];
	for(int i=1; i<=n; i++) cin>>D[i];
	for(int i=1; i<=n; i++) cin>>L[i];
	for(int i=1; i<=n; i++) cin>>R[i];
	s=0,t=n*n+n+n+n+n+1;
	for(int i=1; i<=n; i++) {
		add(s,n*n+i,U[i]);
		add(s,n*n+n+i,D[i]);
		add(s,n*n+n+n+i,L[i]);
		add(s,n*n+n+n+n+i,R[i]);
		for(int j=1; j<=n; j++) add(n*n+i,id[j][i],1),add(n*n+n+i,id[j][i],1);
		for(int j=1; j<=n; j++) add(n*n+n+n+i,id[i][j],1),add(n*n+n+n+n+i,id[i][j],1);
	}
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++) add(id[i][j],t,1);
	int d=dinic();
	if(d<n*n) {
		cout<<"NO"<<endl;
		return 0;
	}
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++) {
			int u=id[i][j];
			for(int k=head[u]; k!=-1; k=edge[k].nxt) {
				int v=edge[k].to;
				if(v==t||edge[k].w==0) continue;
				v=v-n*n-1;
				if(v/n==0) a[i][j]='U';
				if(v/n==1) a[i][j]='D';
				if(v/n==2) a[i][j]='L';
				if(v/n==3) a[i][j]='R';
				break;
			}
		}
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++) {
			if(!vis[i][j]) dfs1(i,j);
		}
}

Submission

Task問題 J - Neue Spiel
User nameユーザ名 victorique
Created time投稿日時
Language言語 C++ (GCC 5.4.1)
Status状態 AC
Score得点 2100
Source lengthソースコード長 3611 Byte
File nameファイル名
Exec time実行時間 208 ms
Memory usageメモリ使用量 19336 KB

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt
dataset1 2000 / 2000 sample-01.txt,sample-02.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,01-20.txt,01-21.txt,01-22.txt,01-23.txt,01-24.txt,01-25.txt,01-26.txt,01-27.txt,01-28.txt,01-29.txt,01-30.txt,01-31.txt,01-32.txt,01-33.txt,01-34.txt,01-35.txt,01-36.txt,01-37.txt,01-38.txt,01-39.txt,01-40.txt,01-41.txt,01-42.txt,01-43.txt,01-44.txt,01-45.txt,01-46.txt
dataset2 100 / 100 sample-01.txt,sample-02.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,01-20.txt,01-21.txt,01-22.txt,01-23.txt,01-24.txt,01-25.txt,01-26.txt,01-27.txt,01-28.txt,01-29.txt,01-30.txt,01-31.txt,01-32.txt,01-33.txt,01-34.txt,01-35.txt,01-36.txt,01-37.txt,01-38.txt,01-39.txt,01-40.txt,01-41.txt,01-42.txt,01-43.txt,01-44.txt,01-45.txt,01-46.txt,02-01.txt,02-02.txt,02-03.txt,02-04.txt,02-05.txt,02-06.txt,02-07.txt,02-08.txt,02-09.txt,02-10.txt,02-11.txt,02-12.txt,sample-01.txt,sample-02.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 2 ms 2816 KB
01-02.txt AC 2 ms 2816 KB
01-03.txt AC 2 ms 2816 KB
01-04.txt AC 2 ms 2816 KB
01-05.txt AC 3 ms 2816 KB
01-06.txt AC 4 ms 2944 KB
01-07.txt AC 5 ms 2944 KB
01-08.txt AC 5 ms 2944 KB
01-09.txt AC 2 ms 2816 KB
01-10.txt AC 5 ms 2944 KB
01-11.txt AC 5 ms 2944 KB
01-12.txt AC 5 ms 2944 KB
01-13.txt AC 5 ms 2944 KB
01-14.txt AC 2 ms 2816 KB
01-15.txt AC 5 ms 2944 KB
01-16.txt AC 5 ms 2944 KB
01-17.txt AC 5 ms 2944 KB
01-18.txt AC 5 ms 2944 KB
01-19.txt AC 2 ms 2816 KB
01-20.txt AC 5 ms 2944 KB
01-21.txt AC 5 ms 2944 KB
01-22.txt AC 3 ms 2816 KB
01-23.txt AC 3 ms 2816 KB
01-24.txt AC 5 ms 2944 KB
01-25.txt AC 5 ms 2944 KB
01-26.txt AC 2 ms 2816 KB
01-27.txt AC 3 ms 2944 KB
01-28.txt AC 5 ms 2944 KB
01-29.txt AC 5 ms 2944 KB
01-30.txt AC 5 ms 2944 KB
01-31.txt AC 3 ms 2816 KB
01-32.txt AC 5 ms 2944 KB
01-33.txt AC 3 ms 2816 KB
01-34.txt AC 5 ms 2944 KB
01-35.txt AC 4 ms 2944 KB
01-36.txt AC 5 ms 2944 KB
01-37.txt AC 2 ms 2816 KB
01-38.txt AC 5 ms 2944 KB
01-39.txt AC 5 ms 2944 KB
01-40.txt AC 5 ms 2944 KB
01-41.txt AC 5 ms 2944 KB
01-42.txt AC 5 ms 2944 KB
01-43.txt AC 5 ms 2944 KB
01-44.txt AC 5 ms 2944 KB
01-45.txt AC 5 ms 2944 KB
01-46.txt AC 5 ms 2944 KB
02-01.txt AC 195 ms 17248 KB
02-02.txt AC 198 ms 19188 KB
02-03.txt AC 203 ms 19296 KB
02-04.txt AC 208 ms 17296 KB
02-05.txt AC 72 ms 18064 KB
02-06.txt AC 64 ms 18076 KB
02-07.txt AC 165 ms 19208 KB
02-08.txt AC 197 ms 19336 KB
02-09.txt AC 167 ms 19208 KB
02-10.txt AC 199 ms 19208 KB
02-11.txt AC 169 ms 19208 KB
02-12.txt AC 206 ms 19208 KB
sample-01.txt AC 2 ms 2816 KB
sample-02.txt AC 2 ms 2816 KB