Submission #409052

# Submission time Handle Problem Language Result Execution time Memory
409052 2021-05-20T06:33:41 Z Kevin_Zhang_TW From Hacks to Snitches (BOI21_watchmen) C++17
35 / 100
1818 ms 77804 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
void debug(auto l, auto r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

using t3 = pair<ll,int>;
const int MAX_N = 300010, MAX_K = 2800, hol = 100;
const ll inf = 1ll << 55;

int n, m;
vector<int> edge[MAX_N], cyc[MAX_N];

ll dp[MAX_N][2];

int cid[MAX_N], csz[MAX_N], cpos[MAX_N];

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	// init everything
	cin >> n >> m;
	for (int a, b, i = 0;i < m;++i) {
		cin >> a >> b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	for (int i = 1;i <= n;++i) {
		dp[i][0] = dp[i][1] = inf;
		csz[i] = 1;
	}
	int K;
	cin >> K;
	for (int i = 1;i <= K;++i) {
		int len;
		cin >> len;
		cyc[i].resize(len);
		int c = 0;
		for (int &u : cyc[i]) {
			cin >> u;
			cid[u] = i;
			csz[u] = len;
			cpos[u] = c++;
		}
	}

	//priority_queue<t3, vector<t3>, greater<t3>> q;
	//queue<pair<ll,int>> q;

	vector<vector<int>> q;
	auto putin = [&](int d, int x) {
		if (q.size() < d + 1) q.resize(d + 1);
		q[d].pb(x);
	};
	// > d, when will x be hit
	auto behit = [&](int x, ll d) {
		if (cid[x] == 0) return inf;
		ll a = d % csz[x], b = cpos[x];
		if (a <= b) return d + b - a;
		return d + csz[x] - a + b;
	};
	auto upd = [&](int x, ll d) {
		auto &A = dp[x][0], &B = dp[x][1];
		bool ch = false;
		if (d < A) {
			putin(d, x);
			swap(d, A);
			ch = true;
		}
		if (cid[x] == 0) return;
		if (d > behit(x, A) && d < B) {
			if (!ch) putin(d, x);
			swap(B, d);
		}
		if (chmin(B, A + csz[x]))
			putin(B, x);
	};

	upd(1, 0);

	// when x's shortest path is d, I want to relax u

	auto valid = [&](int x, ll d, int u) {
		if (cid[u] == 0) return true;
		if (d % csz[u] == cpos[u]) return false;
		if (cid[x] == cid[u] && cpos[u] == (cpos[x] - 1 + csz[x]) % csz[u]) {
			if (d % csz[u] == cpos[x]) return false;
		}
		return true;
	};

	auto relax = [&](int x, int u, ll d) {
		if (cid[u] == 0) {
			upd(u, d+1);
			return;
		};

		DE(x, u, d);
		if (valid(x, d+1, u))
			upd(u, d+1);
		else if (valid(x, d+1, x) && valid(x, d+2, u))
			upd(u, d+2);

		ll xh = behit(x, d), uh = behit(u, d);
		if (xh <= uh + 1) return;
		upd(u, uh + 1);
	};

	// shortest path
	for (int d = 0;d < q.size();++d) {
		for (int i = 0;i < q[d].size();++i) {
			int x = q[d][i];
			if (d != dp[x][0] && d != dp[x][1]) continue;

			if (cid[x]) assert(d % csz[x] != cpos[x]);

			DE(d, x);
			for (int u : edge[x])
				relax(x, u, d);
		}
	}

	ll res = inf;
	for (auto v : dp[n]) chmin(res, v);
	
	if (res == inf) cout << "impossible\n";
	else cout << res << '\n';
}

Compilation message

watchmen.cpp: In lambda function:
watchmen.cpp:62:16: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |   if (q.size() < d + 1) q.resize(d + 1);
      |       ~~~~~~~~~^~~~~~~
watchmen.cpp: In lambda function:
watchmen.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
watchmen.cpp:108:3: note: in expansion of macro 'DE'
  108 |   DE(x, u, d);
      |   ^~
watchmen.cpp: In function 'int32_t main()':
watchmen.cpp:120:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |  for (int d = 0;d < q.size();++d) {
      |                 ~~^~~~~~~~~~
watchmen.cpp:121:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |   for (int i = 0;i < q[d].size();++i) {
      |                  ~~^~~~~~~~~~~~~
watchmen.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
watchmen.cpp:127:4: note: in expansion of macro 'DE'
  127 |    DE(d, x);
      |    ^~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 15300 KB Output is correct
2 Correct 79 ms 25424 KB Output is correct
3 Correct 75 ms 24740 KB Output is correct
4 Correct 76 ms 22700 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 79 ms 22924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 15356 KB Output is correct
2 Correct 79 ms 25460 KB Output is correct
3 Correct 91 ms 24820 KB Output is correct
4 Correct 85 ms 22648 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 68 ms 22980 KB Output is correct
7 Correct 70 ms 24688 KB Output is correct
8 Correct 71 ms 24700 KB Output is correct
9 Correct 75 ms 24624 KB Output is correct
10 Correct 70 ms 21432 KB Output is correct
11 Correct 64 ms 20352 KB Output is correct
12 Correct 82 ms 22952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 15356 KB Output is correct
2 Correct 79 ms 25460 KB Output is correct
3 Correct 91 ms 24820 KB Output is correct
4 Correct 85 ms 22648 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 68 ms 22980 KB Output is correct
7 Correct 70 ms 24688 KB Output is correct
8 Correct 71 ms 24700 KB Output is correct
9 Correct 75 ms 24624 KB Output is correct
10 Correct 70 ms 21432 KB Output is correct
11 Correct 64 ms 20352 KB Output is correct
12 Correct 82 ms 22952 KB Output is correct
13 Correct 27 ms 15348 KB Output is correct
14 Correct 81 ms 25500 KB Output is correct
15 Correct 74 ms 24680 KB Output is correct
16 Correct 81 ms 22652 KB Output is correct
17 Correct 9 ms 14400 KB Output is correct
18 Correct 71 ms 22948 KB Output is correct
19 Correct 88 ms 24784 KB Output is correct
20 Correct 73 ms 24708 KB Output is correct
21 Correct 71 ms 24716 KB Output is correct
22 Correct 67 ms 21348 KB Output is correct
23 Correct 72 ms 20364 KB Output is correct
24 Correct 76 ms 22884 KB Output is correct
25 Correct 1711 ms 70228 KB Output is correct
26 Correct 1600 ms 76032 KB Output is correct
27 Correct 1583 ms 72204 KB Output is correct
28 Correct 1326 ms 77804 KB Output is correct
29 Correct 1572 ms 62016 KB Output is correct
30 Correct 1596 ms 63816 KB Output is correct
31 Correct 1818 ms 74528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 15356 KB Output is correct
2 Correct 79 ms 25460 KB Output is correct
3 Correct 91 ms 24820 KB Output is correct
4 Correct 85 ms 22648 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 68 ms 22980 KB Output is correct
7 Correct 70 ms 24688 KB Output is correct
8 Correct 71 ms 24700 KB Output is correct
9 Correct 75 ms 24624 KB Output is correct
10 Correct 70 ms 21432 KB Output is correct
11 Correct 64 ms 20352 KB Output is correct
12 Correct 82 ms 22952 KB Output is correct
13 Correct 27 ms 15348 KB Output is correct
14 Correct 81 ms 25500 KB Output is correct
15 Correct 74 ms 24680 KB Output is correct
16 Correct 81 ms 22652 KB Output is correct
17 Correct 9 ms 14400 KB Output is correct
18 Correct 71 ms 22948 KB Output is correct
19 Correct 88 ms 24784 KB Output is correct
20 Correct 73 ms 24708 KB Output is correct
21 Correct 71 ms 24716 KB Output is correct
22 Correct 67 ms 21348 KB Output is correct
23 Correct 72 ms 20364 KB Output is correct
24 Correct 76 ms 22884 KB Output is correct
25 Correct 1711 ms 70228 KB Output is correct
26 Correct 1600 ms 76032 KB Output is correct
27 Correct 1583 ms 72204 KB Output is correct
28 Correct 1326 ms 77804 KB Output is correct
29 Correct 1572 ms 62016 KB Output is correct
30 Correct 1596 ms 63816 KB Output is correct
31 Correct 1818 ms 74528 KB Output is correct
32 Correct 27 ms 15356 KB Output is correct
33 Correct 80 ms 25508 KB Output is correct
34 Correct 90 ms 24720 KB Output is correct
35 Correct 70 ms 22688 KB Output is correct
36 Correct 10 ms 14412 KB Output is correct
37 Correct 78 ms 22860 KB Output is correct
38 Correct 101 ms 24676 KB Output is correct
39 Correct 72 ms 24700 KB Output is correct
40 Correct 77 ms 24636 KB Output is correct
41 Correct 70 ms 21348 KB Output is correct
42 Correct 66 ms 20400 KB Output is correct
43 Correct 90 ms 22900 KB Output is correct
44 Correct 1687 ms 69920 KB Output is correct
45 Correct 1705 ms 76020 KB Output is correct
46 Correct 1549 ms 72396 KB Output is correct
47 Correct 1260 ms 77740 KB Output is correct
48 Correct 1605 ms 61964 KB Output is correct
49 Correct 1671 ms 64108 KB Output is correct
50 Correct 1768 ms 74488 KB Output is correct
51 Correct 1670 ms 70852 KB Output is correct
52 Correct 1569 ms 76832 KB Output is correct
53 Correct 1473 ms 72004 KB Output is correct
54 Correct 1131 ms 68868 KB Output is correct
55 Correct 1723 ms 68236 KB Output is correct
56 Correct 1694 ms 60112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 15300 KB Output is correct
2 Correct 79 ms 25424 KB Output is correct
3 Correct 75 ms 24740 KB Output is correct
4 Correct 76 ms 22700 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 79 ms 22924 KB Output is correct
7 Correct 28 ms 15356 KB Output is correct
8 Correct 79 ms 25460 KB Output is correct
9 Correct 91 ms 24820 KB Output is correct
10 Correct 85 ms 22648 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 68 ms 22980 KB Output is correct
13 Correct 70 ms 24688 KB Output is correct
14 Correct 71 ms 24700 KB Output is correct
15 Correct 75 ms 24624 KB Output is correct
16 Correct 70 ms 21432 KB Output is correct
17 Correct 64 ms 20352 KB Output is correct
18 Correct 82 ms 22952 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 10 ms 14412 KB Output is correct
21 Correct 10 ms 14412 KB Output is correct
22 Correct 28 ms 15308 KB Output is correct
23 Correct 82 ms 25448 KB Output is correct
24 Correct 71 ms 24776 KB Output is correct
25 Correct 75 ms 22604 KB Output is correct
26 Correct 9 ms 14412 KB Output is correct
27 Correct 71 ms 22936 KB Output is correct
28 Correct 79 ms 24688 KB Output is correct
29 Correct 74 ms 24660 KB Output is correct
30 Correct 76 ms 24712 KB Output is correct
31 Correct 66 ms 21372 KB Output is correct
32 Correct 63 ms 20444 KB Output is correct
33 Correct 74 ms 23000 KB Output is correct
34 Incorrect 1657 ms 67648 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 15300 KB Output is correct
2 Correct 79 ms 25424 KB Output is correct
3 Correct 75 ms 24740 KB Output is correct
4 Correct 76 ms 22700 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 79 ms 22924 KB Output is correct
7 Correct 28 ms 15356 KB Output is correct
8 Correct 79 ms 25460 KB Output is correct
9 Correct 91 ms 24820 KB Output is correct
10 Correct 85 ms 22648 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 68 ms 22980 KB Output is correct
13 Correct 70 ms 24688 KB Output is correct
14 Correct 71 ms 24700 KB Output is correct
15 Correct 75 ms 24624 KB Output is correct
16 Correct 70 ms 21432 KB Output is correct
17 Correct 64 ms 20352 KB Output is correct
18 Correct 82 ms 22952 KB Output is correct
19 Correct 27 ms 15348 KB Output is correct
20 Correct 81 ms 25500 KB Output is correct
21 Correct 74 ms 24680 KB Output is correct
22 Correct 81 ms 22652 KB Output is correct
23 Correct 9 ms 14400 KB Output is correct
24 Correct 71 ms 22948 KB Output is correct
25 Correct 88 ms 24784 KB Output is correct
26 Correct 73 ms 24708 KB Output is correct
27 Correct 71 ms 24716 KB Output is correct
28 Correct 67 ms 21348 KB Output is correct
29 Correct 72 ms 20364 KB Output is correct
30 Correct 76 ms 22884 KB Output is correct
31 Correct 1711 ms 70228 KB Output is correct
32 Correct 1600 ms 76032 KB Output is correct
33 Correct 1583 ms 72204 KB Output is correct
34 Correct 1326 ms 77804 KB Output is correct
35 Correct 1572 ms 62016 KB Output is correct
36 Correct 1596 ms 63816 KB Output is correct
37 Correct 1818 ms 74528 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 10 ms 14412 KB Output is correct
40 Correct 10 ms 14412 KB Output is correct
41 Correct 28 ms 15308 KB Output is correct
42 Correct 82 ms 25448 KB Output is correct
43 Correct 71 ms 24776 KB Output is correct
44 Correct 75 ms 22604 KB Output is correct
45 Correct 9 ms 14412 KB Output is correct
46 Correct 71 ms 22936 KB Output is correct
47 Correct 79 ms 24688 KB Output is correct
48 Correct 74 ms 24660 KB Output is correct
49 Correct 76 ms 24712 KB Output is correct
50 Correct 66 ms 21372 KB Output is correct
51 Correct 63 ms 20444 KB Output is correct
52 Correct 74 ms 23000 KB Output is correct
53 Incorrect 1657 ms 67648 KB Output isn't correct
54 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 15300 KB Output is correct
2 Correct 79 ms 25424 KB Output is correct
3 Correct 75 ms 24740 KB Output is correct
4 Correct 76 ms 22700 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 79 ms 22924 KB Output is correct
7 Correct 28 ms 15356 KB Output is correct
8 Correct 79 ms 25460 KB Output is correct
9 Correct 91 ms 24820 KB Output is correct
10 Correct 85 ms 22648 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 68 ms 22980 KB Output is correct
13 Correct 70 ms 24688 KB Output is correct
14 Correct 71 ms 24700 KB Output is correct
15 Correct 75 ms 24624 KB Output is correct
16 Correct 70 ms 21432 KB Output is correct
17 Correct 64 ms 20352 KB Output is correct
18 Correct 82 ms 22952 KB Output is correct
19 Correct 27 ms 15348 KB Output is correct
20 Correct 81 ms 25500 KB Output is correct
21 Correct 74 ms 24680 KB Output is correct
22 Correct 81 ms 22652 KB Output is correct
23 Correct 9 ms 14400 KB Output is correct
24 Correct 71 ms 22948 KB Output is correct
25 Correct 88 ms 24784 KB Output is correct
26 Correct 73 ms 24708 KB Output is correct
27 Correct 71 ms 24716 KB Output is correct
28 Correct 67 ms 21348 KB Output is correct
29 Correct 72 ms 20364 KB Output is correct
30 Correct 76 ms 22884 KB Output is correct
31 Correct 1711 ms 70228 KB Output is correct
32 Correct 1600 ms 76032 KB Output is correct
33 Correct 1583 ms 72204 KB Output is correct
34 Correct 1326 ms 77804 KB Output is correct
35 Correct 1572 ms 62016 KB Output is correct
36 Correct 1596 ms 63816 KB Output is correct
37 Correct 1818 ms 74528 KB Output is correct
38 Correct 27 ms 15356 KB Output is correct
39 Correct 80 ms 25508 KB Output is correct
40 Correct 90 ms 24720 KB Output is correct
41 Correct 70 ms 22688 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 78 ms 22860 KB Output is correct
44 Correct 101 ms 24676 KB Output is correct
45 Correct 72 ms 24700 KB Output is correct
46 Correct 77 ms 24636 KB Output is correct
47 Correct 70 ms 21348 KB Output is correct
48 Correct 66 ms 20400 KB Output is correct
49 Correct 90 ms 22900 KB Output is correct
50 Correct 1687 ms 69920 KB Output is correct
51 Correct 1705 ms 76020 KB Output is correct
52 Correct 1549 ms 72396 KB Output is correct
53 Correct 1260 ms 77740 KB Output is correct
54 Correct 1605 ms 61964 KB Output is correct
55 Correct 1671 ms 64108 KB Output is correct
56 Correct 1768 ms 74488 KB Output is correct
57 Correct 1670 ms 70852 KB Output is correct
58 Correct 1569 ms 76832 KB Output is correct
59 Correct 1473 ms 72004 KB Output is correct
60 Correct 1131 ms 68868 KB Output is correct
61 Correct 1723 ms 68236 KB Output is correct
62 Correct 1694 ms 60112 KB Output is correct
63 Correct 10 ms 14412 KB Output is correct
64 Correct 10 ms 14412 KB Output is correct
65 Correct 10 ms 14412 KB Output is correct
66 Correct 28 ms 15308 KB Output is correct
67 Correct 82 ms 25448 KB Output is correct
68 Correct 71 ms 24776 KB Output is correct
69 Correct 75 ms 22604 KB Output is correct
70 Correct 9 ms 14412 KB Output is correct
71 Correct 71 ms 22936 KB Output is correct
72 Correct 79 ms 24688 KB Output is correct
73 Correct 74 ms 24660 KB Output is correct
74 Correct 76 ms 24712 KB Output is correct
75 Correct 66 ms 21372 KB Output is correct
76 Correct 63 ms 20444 KB Output is correct
77 Correct 74 ms 23000 KB Output is correct
78 Incorrect 1657 ms 67648 KB Output isn't correct
79 Halted 0 ms 0 KB -