QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#131295#4606. 无限之环liangbowen100 ✓482ms7872kbC++145.7kb2023-07-26 21:04:472023-07-26 21:05:50

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-26 21:05:50]
  • 评测
  • 测评结果:100
  • 用时:482ms
  • 内存:7872kb
  • [2023-07-26 21:04:47]
  • 提交

answer

/*
网络流牌大模拟!!!
黑白染色,黑色与白色是相反的,下面看黑色 (x+y)%2==1 的情况.
⚠ 四个位置对应 左,下,右,上.

单头型:
 	0001 (上,左)(上,右)1,(上,下)2.
 	0010 (右,上)(右,下)1,(右,左)2.
 	0100 (下,左)(下,右)1,(下,上)2.
	1000 (左,上)(左,下)1,(左,右)2.
直线型:
	1010 不做处理.
	0101 不做处理.
拐弯型:
	1100 (左,右)1,(下,上)1.
	0110 (右,左)1,(下,上)1.
	0011 (右,左)1,(上,下)1.
	1001 (左,右)1,(上,下)1.
三分型:
	1110 (左,上)(右,上)1,(下,上)2
	1101 (上,右)(下,右)1,(左,右)2
	1011 (左,下)(右,下)1,(上,下)2
	0111 (上,左)(下,左)1,(右,左)2
十字型:
	1111 不做处理.
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 114514, inf = 0x3f3f3f3f;
struct Edge {int now, nxt, w, cost;} e[1919810];
int head[N], cur = 1;
void ad(int u, int v, int w, int cost) {e[++cur].now = v, e[cur].nxt = head[u], e[cur].w = w, e[cur].cost = cost, head[u] = cur;}
void add(int u, int v, int w, int cost) {ad(u, v, w, cost), ad(v, u, 0, -cost);}
int s, t;
namespace G {
	int dis[N], icost[N], pre[N]; bool inque[N];
	bool spfa()
	{
		queue <int> q;
		memset(dis, 0x3f, sizeof dis), memset(icost, 0, sizeof icost);
		q.push(s), dis[s] = 0, icost[s] = inf, inque[s] = true;
		while (!q.empty())
		{
			int u = q.front(); q.pop(), inque[u] = false;
			for (int i = head[u]; i; i = e[i].nxt)
			{
				int v = e[i].now;
				if (e[i].w && dis[u] + e[i].cost < dis[v])
				{
					dis[v] = dis[u] + e[i].cost, pre[v] = i, icost[v] = min(icost[u], e[i].w);
					if (!inque[v]) q.push(v), inque[v] = true;
				}
			}
		}
		return icost[t] > 0;
	}
	pair <int, int> EK()
	{
		int cost = 0, ans = 0;
		while (spfa())
		{
			int w = icost[t]; cost += w, ans += w * dis[t];
			for (int i = t; i != s; i = e[pre[i] ^ 1].now) e[pre[i]].w -= w, e[pre[i] ^ 1].w += w;
		}
		return {cost, ans};
	}
}
const string dict[16] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m, flow;
int ME(int x, int y) {return ((x - 1) * m + y) + 0 * n * m;}
int UP(int x, int y) {return ((x - 1) * m + y) + 1 * n * m;}
int DW(int x, int y) {return ((x - 1) * m + y) + 2 * n * m;}
int LF(int x, int y) {return ((x - 1) * m + y) + 3 * n * m;}
int RH(int x, int y) {return ((x - 1) * m + y) + 4 * n * m;}
#define zlt(X, Y, P) add(X(x, y), Y(x, y), 1, P)
#define hxy(X, Y, P) add(Y(x, y), X(x, y), 1, P)
void solveBlack(int x, int y, string op)
{
	//黑点向四周建边
	add(s, ME(x, y), inf, 0); int dx, dy;
	dx = x - 1, dy = y; if (1 <= dx && dx <= n && 1 <= dy && dy <= m) add(UP(x, y), DW(dx, dy), 1, 0);
	dx = x + 1, dy = y; if (1 <= dx && dx <= n && 1 <= dy && dy <= m) add(DW(x, y), UP(dx, dy), 1, 0);
	dx = x, dy = y - 1; if (1 <= dx && dx <= n && 1 <= dy && dy <= m) add(LF(x, y), RH(dx, dy), 1, 0);
	dx = x, dy = y + 1; if (1 <= dx && dx <= n && 1 <= dy && dy <= m) add(RH(x, y), LF(dx, dy), 1, 0);

	//点向可以直接到达的周建边
	if (op[0] == '1') flow++, zlt(ME, LF, 0);
	if (op[1] == '1') flow++, zlt(ME, DW, 0);
	if (op[2] == '1') flow++, zlt(ME, RH, 0);
	if (op[3] == '1') flow++, zlt(ME, UP, 0);

	//单头型
	if (op == "0001") zlt(UP, LF, 1), zlt(UP, RH, 1), zlt(UP, DW, 2);
	if (op == "0010") zlt(RH, UP, 1), zlt(RH, DW, 1), zlt(RH, LF, 2);
	if (op == "0100") zlt(DW, LF, 1), zlt(DW, RH, 1), zlt(DW, UP, 2);
	if (op == "1000") zlt(LF, UP, 1), zlt(LF, DW, 1), zlt(LF, RH, 2);

	//直线型
	if (op == "1010") return;
	if (op == "0101") return;

	//拐弯型
	if (op == "1100") zlt(LF, RH, 1), zlt(DW, UP, 1);
	if (op == "0110") zlt(RH, LF, 1), zlt(DW, UP, 1);
	if (op == "0011") zlt(RH, LF, 1), zlt(UP, DW, 1);
	if (op == "1001") zlt(LF, RH, 1), zlt(UP, DW, 1);

	//三分型
	if (op == "1110") zlt(LF, UP, 1), zlt(RH, UP, 1), zlt(DW, UP, 2);
	if (op == "1101") zlt(UP, RH, 1), zlt(DW, RH, 1), zlt(LF, RH, 2);
	if (op == "1011") zlt(LF, DW, 1), zlt(RH, DW, 1), zlt(UP, DW, 2);
	if (op == "0111") zlt(UP, LF, 1), zlt(DW, LF, 1), zlt(RH, LF, 2);

	//十字型
	if (op == "1111") return;
}
void solveWhite(int x, int y, string op)
{
	add(ME(x, y), t, inf, 0);

	//点向可以直接到达的周建边
	if (op[0] == '1') flow++, hxy(ME, LF, 0);
	if (op[1] == '1') flow++, hxy(ME, DW, 0);
	if (op[2] == '1') flow++, hxy(ME, RH, 0);
	if (op[3] == '1') flow++, hxy(ME, UP, 0);

	//单头型
	if (op == "0001") hxy(UP, LF, 1), hxy(UP, RH, 1), hxy(UP, DW, 2);
	if (op == "0010") hxy(RH, UP, 1), hxy(RH, DW, 1), hxy(RH, LF, 2);
	if (op == "0100") hxy(DW, LF, 1), hxy(DW, RH, 1), hxy(DW, UP, 2);
	if (op == "1000") hxy(LF, UP, 1), hxy(LF, DW, 1), hxy(LF, RH, 2);

	//直线型
	if (op == "1010") return;
	if (op == "0101") return;

	//拐弯型
	if (op == "1100") hxy(LF, RH, 1), hxy(DW, UP, 1);
	if (op == "0110") hxy(RH, LF, 1), hxy(DW, UP, 1);
	if (op == "0011") hxy(RH, LF, 1), hxy(UP, DW, 1);
	if (op == "1001") hxy(LF, RH, 1), hxy(UP, DW, 1);

	//三分型
	if (op == "1110") hxy(LF, UP, 1), hxy(RH, UP, 1), hxy(DW, UP, 2);
	if (op == "1101") hxy(UP, RH, 1), hxy(DW, RH, 1), hxy(LF, RH, 2);
	if (op == "1011") hxy(LF, DW, 1), hxy(RH, DW, 1), hxy(UP, DW, 2);
	if (op == "0111") hxy(UP, LF, 1), hxy(DW, LF, 1), hxy(RH, LF, 2);

	//十字型
	if (op == "1111") return;
}
int main()
{
	scanf("%d%d", &n, &m), s = 0, t = N - 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1, x; j <= m; j++)
		{
			scanf("%d", &x);
			if ((i + j) & 1) solveBlack(i, j, dict[x]);
			else solveWhite(i, j, dict[x]);
		}
	pair <int, int> ans = G::EK();
	cout << (ans.first * 2 != flow ? -1 : ans.second);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 6472kb

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: 5
Accepted
time: 380ms
memory: 5672kb

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:

1272

result:

ok 1 number(s): "1272"

Test #4:

score: 5
Accepted
time: 262ms
memory: 5628kb

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:

1013

result:

ok 1 number(s): "1013"

Test #5:

score: 5
Accepted
time: 286ms
memory: 7724kb

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:

1100

result:

ok 1 number(s): "1100"

Test #6:

score: 5
Accepted
time: 254ms
memory: 6672kb

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:

1064

result:

ok 1 number(s): "1064"

Test #7:

score: 5
Accepted
time: 296ms
memory: 6028kb

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:

1143

result:

ok 1 number(s): "1143"

Test #8:

score: 5
Accepted
time: 215ms
memory: 5976kb

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:

971

result:

ok 1 number(s): "971"

Test #9:

score: 5
Accepted
time: 462ms
memory: 6624kb

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:

1351

result:

ok 1 number(s): "1351"

Test #10:

score: 5
Accepted
time: 453ms
memory: 6624kb

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:

1483

result:

ok 1 number(s): "1483"

Test #11:

score: 5
Accepted
time: 449ms
memory: 6652kb

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:

1330

result:

ok 1 number(s): "1330"

Test #12:

score: 5
Accepted
time: 397ms
memory: 6400kb

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:

1281

result:

ok 1 number(s): "1281"

Test #13:

score: 5
Accepted
time: 467ms
memory: 6752kb

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:

1368

result:

ok 1 number(s): "1368"

Test #14:

score: 5
Accepted
time: 435ms
memory: 7872kb

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:

1432

result:

ok 1 number(s): "1432"

Test #15:

score: 5
Accepted
time: 425ms
memory: 7688kb

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:

1357

result:

ok 1 number(s): "1357"

Test #16:

score: 5
Accepted
time: 482ms
memory: 6740kb

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:

1445

result:

ok 1 number(s): "1445"

Test #17:

score: 5
Accepted
time: 432ms
memory: 6692kb

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:

1364

result:

ok 1 number(s): "1364"

Test #18:

score: 5
Accepted
time: 461ms
memory: 6628kb

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:

1399

result:

ok 1 number(s): "1399"

Test #19:

score: 5
Accepted
time: 399ms
memory: 5952kb

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:

1281

result:

ok 1 number(s): "1281"

Test #20:

score: 5
Accepted
time: 426ms
memory: 6744kb

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:

1319

result:

ok 1 number(s): "1319"

Extra Test:

score: 0
Extra Test Passed