QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71774#4606. 无限之环He_Ren10 438ms4020kbC++144.0kb2023-01-12 01:06:392023-01-12 01:06:40

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-12 01:06:40]
  • 评测
  • 测评结果:10
  • 用时:438ms
  • 内存:4020kb
  • [2023-01-12 01:06:39]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXP = 2e3 + 5;
const int dx[] = {-1,0,1,0};// U, R, D, L
const int dy[] = {0,1,0,-1};
const int inf = 0x3f3f3f3f;

template<const int MAXN,const int MAXM>
struct MCMF
{
	struct Edge
	{
		int next,to,flow,w;
	}e[MAXM<<1];
	int head[MAXN],etot;
	inline void add_one(int u,int v,int flow,int w)
	{
		e[++etot]=(Edge){ head[u],v,flow,w};
		head[u]=etot;
	}
	inline void add_edge(int u,int v,int flow,int w)
	{
		add_one(u,v,flow,w); add_one(v,u,0,-w);
	}
	
	int n,s,t;
	int dis[MAXN], h[MAXN], fl[MAXN], pre[MAXN];
	bool vis[MAXN];
	inline void clear(void){ etot=-1; memset(head,-1,sizeof(head));}
	inline void init(int _n,int _s,int _t){ n=_n; s=_s; t=_t;}
	MCMF(void){ clear();}
	inline bool spfa(void)
	{
		memset(dis,0x3f,(n+1)<<2); memset(vis,0,n+1);
		queue<int> q;
		fl[s] = inf;
		dis[s] = 0; vis[s] = 1; q.push(s);
		while(q.size())
		{
			int u=q.front(); q.pop();
			vis[u] = 0;
			for(int i=head[u]; ~i; i=e[i].next) if(e[i].flow)
			{
				int v = e[i].to, nxt = dis[u] + e[i].w;
				if(nxt < dis[v])
				{
					dis[v] = nxt; pre[v] = i;
					fl[v] = min(fl[u], e[i].flow);
					if(!vis[v]) q.push(v), vis[v] = 1;
				}
			}
		}
		return dis[t] != inf;
	}
	inline bool dijkstra(void)
	{
		memset(dis,0x3f,(n+1)<<2); memset(vis,0,n+1);
		priority_queue<pii> q;
		fl[s] = inf;
		dis[s] = 0; q.emplace(0, s);
		while(q.size())
		{
			int u=q.top().second; q.pop();
			if(vis[u]) continue;
			vis[u]=1;
			for(int i=head[u]; ~i; i=e[i].next) if(e[i].flow)
			{
				int v = e[i].to, nxt = dis[u] + e[i].w + h[u] - h[v];
				if(nxt < dis[v])
				{
					dis[v] = nxt; pre[v] = i;
					fl[v] = min(fl[u], e[i].flow);
					q.emplace(-dis[v], v);
				}
			}
		}
		return vis[t];
	}
	pii build(void)
	{
		memset(h,0,(n+1)<<2);
		int resf = 0, resc = 0;
		if(spfa()) do
		{
			for(int i=0; i<=n; ++i)
				h[i] = min(inf, h[i] + dis[i]);
			resc += fl[t] * h[t]; resf += fl[t];
			for(int u=t; u!=s; u=e[pre[u]^1].to)
				e[pre[u]].flow -= fl[t],
				e[pre[u]^1].flow += fl[t];
		}while(dijkstra());
		return {resf, resc};
	}
};

MCMF<MAXP, MAXP * 6> mcmf;

int n,m;
inline bool isin(int x,int y){ return x>=0 && x<n && y>=0 && y<m;}
inline int encode(int x,int y){ return x * m + y;}
inline void decode(int u,int &x,int &y){ x=u/m; y=u%m;}
inline int walk(int u,int k)
{
	int x = u/m + dx[k], y = u%m + dy[k];
	return isin(x,y)? encode(x,y): -1;
}

#define bbit(i) (1<<(i))
#define bdig(x,i) (((x)>>(i))&1)

int a[MAXP], num1[MAXP], type[MAXP];

int main(void)
{
	scanf("%d%d",&n,&m);
	int pcnt = n * m;
	for(int i=0; i<pcnt; ++i) scanf("%d",&a[i]);
	
	int S = pcnt, T = pcnt+1;
	mcmf.init(T, S, T);
	
	int sum1 = 0, sum2 = 0;
	for(int i=0; i<pcnt; ++i)
	{
		int k = num1[i] = __builtin_popcount(a[i]);
		int x,y; decode(i,x,y);
		type[i] = (x^y)&1;
		
		if(type[i]) mcmf.add_edge(S, i, k, 0), sum1 += k;
		else mcmf.add_edge(i, T, k, 0), sum2 += k;
	}
	if(sum1 != sum2)
	{
		printf("-1");
		return 0;
	}
	
	auto getcost = [&] (int u,int k)
	{
		if(num1[u] == 1)
		{
			if(bdig(a[u], k) == 1)
				return 0;
			else if(bdig(a[u], k^2) == 1)
				return 2;
			else
				return 1;
		}
		else if(num1[u] == 2)
		{
			if(a[u] == 8+2 || a[u] == 4+1)
				return bdig(a[u], k)? 0: inf;
			else
				return bdig(a[u], k)? 0: 1;
		}
		else if(num1[u] == 3)
		{
			if(bdig(a[u], k) == 0)
				return 1;
			else if(bdig(a[u], k^2) == 0)
				return -1;
			else
				return 0;
		}
		else if(num1[u] == 4)
			return 0;
		else
			return inf;
	};
	
	for(int u=0; u<pcnt; ++u) if(type[u])
		for(int k=0; k<4; ++k)
		{
			int v = walk(u,k);
			if(v == -1) continue;
			int c = getcost(u, k) + getcost(v, k^2);
			if(c >= inf / 2) continue;
			mcmf.add_edge(u, v, 1, c);
		}
	
	int has = count(num1, num1+pcnt, 3);
	pii res = mcmf.build();
	
	if(res.first != sum1)
		printf("-1");
	else
		printf("%d",res.second + has);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 2ms
memory: 3820kb

input:

4 4
8 2 2 0
5 8 11 9
5 12 14 11
12 6 8 12

output:

17

result:

ok 1 number(s): "17"

Test #2:

score: 5
Accepted
time: 2ms
memory: 3756kb

input:

4 4
9 3 1 9
14 3 12 7
11 13 14 9
3 3 0 0

output:

16

result:

ok 1 number(s): "16"

Test #3:

score: 0
Wrong Answer
time: 347ms
memory: 3856kb

input:

14 124
6 8 3 10 10 7 2 6 11 10 2 1 3 1 14 10 10 14 10 4 8 13 1 8 2 2 0 0 12 7 8 4 10 4 0 0 9 10 13 2 4 13 14 12 9 1 2 8 1 0 6 6 4 4 4 2 2 2 10 1 1 13 2 4 0 2 7 3 6 14 4 4 9 10 9 4 4 2 6 1 4 4 10 4 2 10 10 1 1 8 6 11 10 13 6 9 11 2 8 4 0 6 9 6 2 2 14 7 14 3 12 7 14 14 10 13 1 8 12 8 9 8 3 3
1 8 15 10...

output:

1221

result:

wrong answer 1st numbers differ - expected: '1272', found: '1221'

Test #4:

score: 0
Wrong Answer
time: 249ms
memory: 3828kb

input:

13 113
0 1 11 12 2 3 11 11 14 4 1 6 8 0 1 1 8 8 8 1 0 0 0 12 12 0 1 8 0 0 2 6 9 11 1 0 8 3 0 9 4 4 0 2 6 13 12 4 9 6 6 4 0 0 12 7 1 3 4 0 0 0 2 3 8 2 0 0 6 10 14 2 6 11 4 2 0 9 13 10 7 9 2 12 10 12 0 3 3 2 0 0 2 4 0 0 6 10 14 6 3 11 14 10 4 4 3 10 9 2 14 4 0
3 10 12 9 15 15 7 5 4 2 14 12 6 8 8 9 3 6...

output:

969

result:

wrong answer 1st numbers differ - expected: '1013', found: '969'

Test #5:

score: 0
Wrong Answer
time: 264ms
memory: 3832kb

input:

114 13
0 12 6 1 8 3 10 14 3 12 11 6 2
8 5 12 4 0 12 14 9 5 13 14 14 11
4 13 6 2 10 12 4 9 7 15 7 15 11
1 9 1 0 6 7 10 15 10 13 1 4 5
8 0 2 1 15 9 2 1 0 11 15 3 8
11 8 13 6 14 13 8 8 10 7 3 11 2
14 7 7 13 8 2 3 13 11 15 14 3 8
5 8 0 2 4 4 13 5 5 13 15 14 4
9 1 0 1 11 6 6 15 14 5 7 15 4
2 6 8 8 1 11 1...

output:

1058

result:

wrong answer 1st numbers differ - expected: '1100', found: '1058'

Test #6:

score: 0
Wrong Answer
time: 243ms
memory: 3776kb

input:

103 14
0 0 4 8 9 11 9 6 2 4 2 0 9 8
1 1 5 11 14 12 14 11 0 5 11 10 15 9
5 6 11 5 9 10 11 15 12 4 4 0 1 5
11 10 15 13 5 1 10 15 14 1 4 6 9 5
14 13 15 6 12 10 14 15 7 1 1 13 13 5
5 13 15 4 2 10 3 5 0 2 9 10 15 12
3 15 14 3 4 2 0 7 6 5 13 13 13 3
1 11 4 12 4 9 10 12 8 5 8 6 10 7
4 12 3 10 14 15 7 14 10...

output:

1037

result:

wrong answer 1st numbers differ - expected: '1064', found: '1037'

Test #7:

score: 0
Wrong Answer
time: 276ms
memory: 3924kb

input:

111 14
0 9 10 1 2 0 4 3 2 4 10 2 0 1
4 5 0 0 5 4 14 14 3 4 9 14 11 12
5 5 0 3 13 2 11 7 4 4 11 7 6 0
1 6 10 15 7 1 13 11 7 14 11 1 0 0
4 1 4 6 1 0 5 8 14 7 7 4 4 0
5 7 10 3 4 8 13 6 9 4 14 7 10 9
8 8 0 5 0 0 6 6 0 0 5 4 0 8
8 10 4 2 0 2 15 15 10 10 7 1 0 0
12 10 9 0 8 4 14 9 3 10 10 1 4 0
14 10 13 0...

output:

1106

result:

wrong answer 1st numbers differ - expected: '1143', found: '1106'

Test #8:

score: 0
Wrong Answer
time: 196ms
memory: 3932kb

input:

13 100
0 0 9 12 3 1 1 7 8 0 9 12 8 2 3 1 0 3 12 0 8 6 2 1 12 6 0 9 9 6 10 13 2 12 7 12 4 9 10 4 8 8 6 10 3 0 4 12 10 7 6 9 11 13 13 10 6 6 3 1 6 4 8 12 14 12 8 2 1 11 2 0 2 7 8 0 9 12 1 8 2 0 12 8 0 2 6 1 14 10 14 14 2 2 12 10 12 4 1 1
0 0 5 5 2 1 6 11 0 9 11 12 8 1 11 15 14 15 15 2 4 12 8 9 11 15 8...

output:

927

result:

wrong answer 1st numbers differ - expected: '971', found: '927'

Test #9:

score: 0
Wrong Answer
time: 420ms
memory: 3960kb

input:

43 44
6 11 6 9 2 0 3 1 6 13 10 7 1 8 4 13 1 6 6 0 3 10 3 0 3 7 11 3 4 8 4 4 3 7 10 7 10 9 8 4 8 1 8 4
9 15 11 15 9 6 13 1 7 13 13 11 7 15 9 5 1 15 12 6 7 12 15 13 13 11 9 7 9 12 10 7 15 13 14 15 4 1 1 8 12 7 5 5
2 11 8 9 3 12 4 0 9 13 14 3 7 15 12 4 6 13 0 5 12 15 14 12 1 2 4 15 6 0 8 9 7 10 7 9 9 6...

output:

1306

result:

wrong answer 1st numbers differ - expected: '1351', found: '1306'

Test #10:

score: 0
Wrong Answer
time: 422ms
memory: 4020kb

input:

44 44
1 8 0 0 2 8 1 0 0 9 10 12 0 12 11 3 8 14 4 3 11 2 0 2 4 1 9 8 12 9 9 1 0 12 9 2 10 10 10 10 3 0 1 8
1 0 0 4 11 12 2 1 0 1 9 15 2 14 15 15 10 11 3 13 15 1 0 0 0 0 11 14 11 13 15 10 7 15 15 14 10 10 10 4 4 0 9 9
14 1 6 15 7 5 2 9 10 1 5 13 2 9 15 12 1 1 7 7 7 9 4 14 10 9 1 12 7 14 14 13 14 15 11...

output:

1441

result:

wrong answer 1st numbers differ - expected: '1483', found: '1441'

Test #11:

score: 0
Wrong Answer
time: 399ms
memory: 3960kb

input:

43 43
1 10 1 1 9 1 4 0 4 12 14 10 13 14 14 1 6 7 6 2 4 4 3 6 7 1 0 4 13 3 3 10 10 2 2 10 14 10 10 13 10 12 0
3 8 9 15 11 11 7 9 15 14 8 0 5 6 11 2 5 3 11 8 8 8 5 5 3 10 1 8 3 12 5 9 10 7 6 0 1 4 11 14 9 7 3
14 8 5 8 8 14 14 6 2 1 1 12 6 10 7 12 11 10 10 10 7 6 5 5 9 8 0 1 0 2 2 4 0 9 11 10 10 3 8 1 ...

output:

1303

result:

wrong answer 1st numbers differ - expected: '1330', found: '1303'

Test #12:

score: 0
Wrong Answer
time: 365ms
memory: 3884kb

input:

42 42
8 14 13 10 14 6 0 0 2 12 10 8 8 6 6 1 0 0 1 3 1 10 10 10 10 13 9 1 10 12 6 7 14 8 8 2 7 2 4 0 9 1
2 11 11 8 14 3 2 3 11 8 2 1 5 14 15 15 10 10 3 6 8 12 10 2 12 14 15 10 13 15 13 2 5 6 7 1 3 12 1 12 14 0
0 11 15 10 15 10 15 15 13 7 7 6 11 15 15 7 1 2 12 10 9 6 9 0 7 7 14 4 11 14 9 1 6 7 10 8 4 ...

output:

1212

result:

wrong answer 1st numbers differ - expected: '1281', found: '1212'

Test #13:

score: 0
Wrong Answer
time: 424ms
memory: 3868kb

input:

43 43
4 1 0 2 0 0 3 10 7 13 4 6 10 10 9 4 7 1 0 4 11 4 12 9 8 0 3 10 4 1 14 14 4 0 0 3 10 4 1 8 1 2 1
0 8 0 7 9 3 15 8 9 15 10 15 12 6 3 4 13 8 4 6 13 10 14 1 11 3 9 10 6 8 4 9 10 10 10 13 1 1 6 4 3 11 5
8 14 2 14 11 14 11 0 8 8 8 6 9 15 14 1 5 3 11 7 3 8 15 10 11 11 9 2 15 6 0 1 6 8 13 13 14 5 0 1 ...

output:

1312

result:

wrong answer 1st numbers differ - expected: '1368', found: '1312'

Test #14:

score: 0
Wrong Answer
time: 422ms
memory: 3864kb

input:

42 44
4 12 6 2 2 1 6 10 14 7 4 2 1 0 1 14 10 1 3 9 8 8 4 0 2 4 9 2 4 8 3 0 4 8 10 13 8 9 2 9 8 0 4 1
2 3 11 4 5 5 8 2 11 15 6 9 13 8 2 7 8 0 2 5 3 13 10 14 10 4 11 13 12 0 5 12 6 0 12 6 9 14 2 4 0 9 11 4
0 0 5 2 6 5 6 12 12 7 5 1 3 2 0 3 14 0 8 13 2 9 9 2 9 4 5 13 11 4 1 14 9 3 6 3 14 3 7 14 8 5 3 1...

output:

1368

result:

wrong answer 1st numbers differ - expected: '1432', found: '1368'

Test #15:

score: 0
Wrong Answer
time: 388ms
memory: 3896kb

input:

43 42
0 6 3 2 3 8 0 4 9 6 9 8 9 8 3 9 8 0 1 2 10 11 6 2 13 10 9 0 9 10 9 4 10 10 2 4 3 3 10 2 4 6
4 11 7 11 7 8 0 0 13 11 12 6 9 12 14 11 5 12 7 8 1 5 5 6 14 10 14 0 5 9 7 8 10 10 1 0 11 15 2 0 0 2
1 11 10 11 13 14 6 7 3 14 3 8 0 3 12 6 7 5 3 14 13 5 14 12 4 0 1 6 12 12 15 7 12 2 1 4 13 7 13 7 3 0
8...

output:

1296

result:

wrong answer 1st numbers differ - expected: '1357', found: '1296'

Test #16:

score: 0
Wrong Answer
time: 438ms
memory: 3836kb

input:

44 43
8 11 10 13 10 10 13 10 10 7 8 0 6 2 12 10 10 2 9 4 9 10 1 1 8 14 11 8 8 1 6 8 8 1 3 3 4 3 10 10 14 6 2
0 5 1 5 2 10 14 11 11 15 6 2 14 9 8 12 8 0 5 4 13 11 2 12 12 8 4 0 9 3 4 2 0 0 14 15 2 12 10 10 3 7 12
9 15 11 6 8 10 11 6 5 13 12 0 8 5 6 9 0 8 11 12 11 13 14 12 6 10 10 11 9 5 9 7 3 9 2 4 2...

output:

1371

result:

wrong answer 1st numbers differ - expected: '1445', found: '1371'

Test #17:

score: 0
Wrong Answer
time: 391ms
memory: 3956kb

input:

43 42
8 8 12 8 14 11 14 10 11 13 10 11 12 0 2 3 14 10 6 8 1 4 8 10 9 8 0 3 6 0 2 1 0 8 10 14 1 4 3 6 8 2
12 6 5 6 14 5 1 1 13 7 6 3 14 2 5 3 7 0 5 2 14 11 7 11 15 15 4 9 15 10 9 6 10 12 3 15 10 7 14 7 13 12
0 11 6 1 8 12 4 2 7 14 9 1 3 8 9 1 1 4 13 1 4 4 13 15 13 5 2 1 2 8 0 4 4 9 14 1 3 3 5 7 10 9
...

output:

1297

result:

wrong answer 1st numbers differ - expected: '1364', found: '1297'

Test #18:

score: 0
Wrong Answer
time: 437ms
memory: 3960kb

input:

42 44
8 3 10 8 1 8 6 10 7 14 10 12 0 6 12 4 10 10 10 7 7 11 10 14 10 10 2 9 12 0 4 12 8 1 0 12 12 1 0 0 0 4 1 2
13 11 1 0 13 14 9 4 13 11 10 11 1 4 6 3 9 2 2 4 3 9 4 3 0 6 10 6 1 1 5 1 4 7 3 4 4 9 6 1 2 6 1 8
11 6 3 10 14 8 13 3 1 14 10 10 10 10 6 5 12 10 7 8 12 14 11 13 10 6 1 8 14 6 1 12 6 11 15 2...

output:

1327

result:

wrong answer 1st numbers differ - expected: '1399', found: '1327'

Test #19:

score: 0
Wrong Answer
time: 387ms
memory: 3816kb

input:

43 42
4 0 0 2 2 6 12 1 4 6 10 10 10 10 4 0 0 8 2 8 6 12 0 0 6 6 3 12 4 1 6 8 4 6 3 10 6 4 8 8 11 3
5 0 0 12 14 9 3 15 15 11 9 1 4 12 1 2 3 10 11 7 11 13 6 8 6 9 12 15 1 0 5 3 10 6 3 11 14 0 8 8 5 2
5 0 0 5 9 1 0 5 4 5 1 4 6 4 4 9 9 13 15 14 13 15 9 6 13 10 12 3 6 8 3 12 8 1 0 8 12 6 0 0 13 3
5 3 14 ...

output:

1241

result:

wrong answer 1st numbers differ - expected: '1281', found: '1241'

Test #20:

score: 0
Wrong Answer
time: 388ms
memory: 3924kb

input:

42 43
2 1 2 1 3 12 4 8 8 9 7 8 4 8 10 12 8 8 12 7 3 2 2 1 0 2 11 9 12 2 6 10 7 3 0 1 2 4 1 3 10 10 12
4 5 4 10 6 14 9 11 13 5 14 12 8 2 12 15 14 13 15 15 11 9 7 7 3 13 14 3 6 7 11 0 5 4 2 14 11 4 6 3 12 10 9
0 11 12 4 10 14 3 6 7 13 5 5 4 15 9 7 10 13 6 7 13 10 3 3 11 14 12 10 2 14 7 10 9 12 12 7 6 ...

output:

1268

result:

wrong answer 1st numbers differ - expected: '1319', found: '1268'