답안 #409079

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409079 2021-05-20T07:23:41 Z Kevin_Zhang_TW From Hacks to Snitches (BOI21_watchmen) C++17
35 / 100
1945 ms 78036 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);
		if (valid(x, d + csz[x] + 1, u))
			upd(u, d + csz[x] + 1);
				

		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;

			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:123:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |  for (int d = 0;d < q.size();++d) {
      |                 ~~^~~~~~~~~~
watchmen.cpp:124:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |   for (int i = 0;i < q[d].size();++i) {
      |                  ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 15564 KB Output is correct
2 Correct 75 ms 25712 KB Output is correct
3 Correct 78 ms 25028 KB Output is correct
4 Correct 74 ms 22864 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 69 ms 23212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 15560 KB Output is correct
2 Correct 79 ms 25792 KB Output is correct
3 Correct 89 ms 25076 KB Output is correct
4 Correct 72 ms 22884 KB Output is correct
5 Correct 9 ms 14328 KB Output is correct
6 Correct 75 ms 23300 KB Output is correct
7 Correct 74 ms 24972 KB Output is correct
8 Correct 91 ms 25096 KB Output is correct
9 Correct 76 ms 24916 KB Output is correct
10 Correct 73 ms 21640 KB Output is correct
11 Correct 67 ms 20640 KB Output is correct
12 Correct 68 ms 23188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 15560 KB Output is correct
2 Correct 79 ms 25792 KB Output is correct
3 Correct 89 ms 25076 KB Output is correct
4 Correct 72 ms 22884 KB Output is correct
5 Correct 9 ms 14328 KB Output is correct
6 Correct 75 ms 23300 KB Output is correct
7 Correct 74 ms 24972 KB Output is correct
8 Correct 91 ms 25096 KB Output is correct
9 Correct 76 ms 24916 KB Output is correct
10 Correct 73 ms 21640 KB Output is correct
11 Correct 67 ms 20640 KB Output is correct
12 Correct 68 ms 23188 KB Output is correct
13 Correct 33 ms 15620 KB Output is correct
14 Correct 80 ms 25892 KB Output is correct
15 Correct 90 ms 25016 KB Output is correct
16 Correct 71 ms 22860 KB Output is correct
17 Correct 9 ms 14436 KB Output is correct
18 Correct 76 ms 23224 KB Output is correct
19 Correct 74 ms 25024 KB Output is correct
20 Correct 90 ms 24864 KB Output is correct
21 Correct 70 ms 24932 KB Output is correct
22 Correct 71 ms 21660 KB Output is correct
23 Correct 63 ms 20708 KB Output is correct
24 Correct 70 ms 23208 KB Output is correct
25 Correct 1760 ms 70200 KB Output is correct
26 Correct 1611 ms 76288 KB Output is correct
27 Correct 1565 ms 72436 KB Output is correct
28 Correct 1222 ms 77996 KB Output is correct
29 Correct 1651 ms 62324 KB Output is correct
30 Correct 1587 ms 64200 KB Output is correct
31 Correct 1701 ms 74484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 15560 KB Output is correct
2 Correct 79 ms 25792 KB Output is correct
3 Correct 89 ms 25076 KB Output is correct
4 Correct 72 ms 22884 KB Output is correct
5 Correct 9 ms 14328 KB Output is correct
6 Correct 75 ms 23300 KB Output is correct
7 Correct 74 ms 24972 KB Output is correct
8 Correct 91 ms 25096 KB Output is correct
9 Correct 76 ms 24916 KB Output is correct
10 Correct 73 ms 21640 KB Output is correct
11 Correct 67 ms 20640 KB Output is correct
12 Correct 68 ms 23188 KB Output is correct
13 Correct 33 ms 15620 KB Output is correct
14 Correct 80 ms 25892 KB Output is correct
15 Correct 90 ms 25016 KB Output is correct
16 Correct 71 ms 22860 KB Output is correct
17 Correct 9 ms 14436 KB Output is correct
18 Correct 76 ms 23224 KB Output is correct
19 Correct 74 ms 25024 KB Output is correct
20 Correct 90 ms 24864 KB Output is correct
21 Correct 70 ms 24932 KB Output is correct
22 Correct 71 ms 21660 KB Output is correct
23 Correct 63 ms 20708 KB Output is correct
24 Correct 70 ms 23208 KB Output is correct
25 Correct 1760 ms 70200 KB Output is correct
26 Correct 1611 ms 76288 KB Output is correct
27 Correct 1565 ms 72436 KB Output is correct
28 Correct 1222 ms 77996 KB Output is correct
29 Correct 1651 ms 62324 KB Output is correct
30 Correct 1587 ms 64200 KB Output is correct
31 Correct 1701 ms 74484 KB Output is correct
32 Correct 29 ms 15624 KB Output is correct
33 Correct 81 ms 25720 KB Output is correct
34 Correct 108 ms 25060 KB Output is correct
35 Correct 74 ms 22964 KB Output is correct
36 Correct 9 ms 14356 KB Output is correct
37 Correct 74 ms 23216 KB Output is correct
38 Correct 85 ms 24940 KB Output is correct
39 Correct 75 ms 24976 KB Output is correct
40 Correct 74 ms 24984 KB Output is correct
41 Correct 67 ms 21664 KB Output is correct
42 Correct 67 ms 20676 KB Output is correct
43 Correct 68 ms 23152 KB Output is correct
44 Correct 1684 ms 70056 KB Output is correct
45 Correct 1569 ms 76180 KB Output is correct
46 Correct 1469 ms 72600 KB Output is correct
47 Correct 1221 ms 78036 KB Output is correct
48 Correct 1603 ms 62200 KB Output is correct
49 Correct 1618 ms 64052 KB Output is correct
50 Correct 1725 ms 74752 KB Output is correct
51 Correct 1691 ms 71172 KB Output is correct
52 Correct 1602 ms 76864 KB Output is correct
53 Correct 1513 ms 72080 KB Output is correct
54 Correct 1142 ms 68964 KB Output is correct
55 Correct 1945 ms 68240 KB Output is correct
56 Correct 1835 ms 59964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 15564 KB Output is correct
2 Correct 75 ms 25712 KB Output is correct
3 Correct 78 ms 25028 KB Output is correct
4 Correct 74 ms 22864 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 69 ms 23212 KB Output is correct
7 Correct 29 ms 15560 KB Output is correct
8 Correct 79 ms 25792 KB Output is correct
9 Correct 89 ms 25076 KB Output is correct
10 Correct 72 ms 22884 KB Output is correct
11 Correct 9 ms 14328 KB Output is correct
12 Correct 75 ms 23300 KB Output is correct
13 Correct 74 ms 24972 KB Output is correct
14 Correct 91 ms 25096 KB Output is correct
15 Correct 76 ms 24916 KB Output is correct
16 Correct 73 ms 21640 KB Output is correct
17 Correct 67 ms 20640 KB Output is correct
18 Correct 68 ms 23188 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 8 ms 14412 KB Output is correct
21 Correct 9 ms 14428 KB Output is correct
22 Correct 30 ms 15408 KB Output is correct
23 Correct 79 ms 25648 KB Output is correct
24 Correct 70 ms 24928 KB Output is correct
25 Correct 74 ms 22824 KB Output is correct
26 Correct 10 ms 14408 KB Output is correct
27 Correct 72 ms 23052 KB Output is correct
28 Correct 78 ms 24912 KB Output is correct
29 Correct 71 ms 24740 KB Output is correct
30 Correct 71 ms 24872 KB Output is correct
31 Correct 75 ms 21456 KB Output is correct
32 Correct 63 ms 20540 KB Output is correct
33 Correct 68 ms 23096 KB Output is correct
34 Incorrect 1685 ms 67804 KB Output isn't correct
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 15564 KB Output is correct
2 Correct 75 ms 25712 KB Output is correct
3 Correct 78 ms 25028 KB Output is correct
4 Correct 74 ms 22864 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 69 ms 23212 KB Output is correct
7 Correct 29 ms 15560 KB Output is correct
8 Correct 79 ms 25792 KB Output is correct
9 Correct 89 ms 25076 KB Output is correct
10 Correct 72 ms 22884 KB Output is correct
11 Correct 9 ms 14328 KB Output is correct
12 Correct 75 ms 23300 KB Output is correct
13 Correct 74 ms 24972 KB Output is correct
14 Correct 91 ms 25096 KB Output is correct
15 Correct 76 ms 24916 KB Output is correct
16 Correct 73 ms 21640 KB Output is correct
17 Correct 67 ms 20640 KB Output is correct
18 Correct 68 ms 23188 KB Output is correct
19 Correct 33 ms 15620 KB Output is correct
20 Correct 80 ms 25892 KB Output is correct
21 Correct 90 ms 25016 KB Output is correct
22 Correct 71 ms 22860 KB Output is correct
23 Correct 9 ms 14436 KB Output is correct
24 Correct 76 ms 23224 KB Output is correct
25 Correct 74 ms 25024 KB Output is correct
26 Correct 90 ms 24864 KB Output is correct
27 Correct 70 ms 24932 KB Output is correct
28 Correct 71 ms 21660 KB Output is correct
29 Correct 63 ms 20708 KB Output is correct
30 Correct 70 ms 23208 KB Output is correct
31 Correct 1760 ms 70200 KB Output is correct
32 Correct 1611 ms 76288 KB Output is correct
33 Correct 1565 ms 72436 KB Output is correct
34 Correct 1222 ms 77996 KB Output is correct
35 Correct 1651 ms 62324 KB Output is correct
36 Correct 1587 ms 64200 KB Output is correct
37 Correct 1701 ms 74484 KB Output is correct
38 Correct 9 ms 14412 KB Output is correct
39 Correct 8 ms 14412 KB Output is correct
40 Correct 9 ms 14428 KB Output is correct
41 Correct 30 ms 15408 KB Output is correct
42 Correct 79 ms 25648 KB Output is correct
43 Correct 70 ms 24928 KB Output is correct
44 Correct 74 ms 22824 KB Output is correct
45 Correct 10 ms 14408 KB Output is correct
46 Correct 72 ms 23052 KB Output is correct
47 Correct 78 ms 24912 KB Output is correct
48 Correct 71 ms 24740 KB Output is correct
49 Correct 71 ms 24872 KB Output is correct
50 Correct 75 ms 21456 KB Output is correct
51 Correct 63 ms 20540 KB Output is correct
52 Correct 68 ms 23096 KB Output is correct
53 Incorrect 1685 ms 67804 KB Output isn't correct
54 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 15564 KB Output is correct
2 Correct 75 ms 25712 KB Output is correct
3 Correct 78 ms 25028 KB Output is correct
4 Correct 74 ms 22864 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 69 ms 23212 KB Output is correct
7 Correct 29 ms 15560 KB Output is correct
8 Correct 79 ms 25792 KB Output is correct
9 Correct 89 ms 25076 KB Output is correct
10 Correct 72 ms 22884 KB Output is correct
11 Correct 9 ms 14328 KB Output is correct
12 Correct 75 ms 23300 KB Output is correct
13 Correct 74 ms 24972 KB Output is correct
14 Correct 91 ms 25096 KB Output is correct
15 Correct 76 ms 24916 KB Output is correct
16 Correct 73 ms 21640 KB Output is correct
17 Correct 67 ms 20640 KB Output is correct
18 Correct 68 ms 23188 KB Output is correct
19 Correct 33 ms 15620 KB Output is correct
20 Correct 80 ms 25892 KB Output is correct
21 Correct 90 ms 25016 KB Output is correct
22 Correct 71 ms 22860 KB Output is correct
23 Correct 9 ms 14436 KB Output is correct
24 Correct 76 ms 23224 KB Output is correct
25 Correct 74 ms 25024 KB Output is correct
26 Correct 90 ms 24864 KB Output is correct
27 Correct 70 ms 24932 KB Output is correct
28 Correct 71 ms 21660 KB Output is correct
29 Correct 63 ms 20708 KB Output is correct
30 Correct 70 ms 23208 KB Output is correct
31 Correct 1760 ms 70200 KB Output is correct
32 Correct 1611 ms 76288 KB Output is correct
33 Correct 1565 ms 72436 KB Output is correct
34 Correct 1222 ms 77996 KB Output is correct
35 Correct 1651 ms 62324 KB Output is correct
36 Correct 1587 ms 64200 KB Output is correct
37 Correct 1701 ms 74484 KB Output is correct
38 Correct 29 ms 15624 KB Output is correct
39 Correct 81 ms 25720 KB Output is correct
40 Correct 108 ms 25060 KB Output is correct
41 Correct 74 ms 22964 KB Output is correct
42 Correct 9 ms 14356 KB Output is correct
43 Correct 74 ms 23216 KB Output is correct
44 Correct 85 ms 24940 KB Output is correct
45 Correct 75 ms 24976 KB Output is correct
46 Correct 74 ms 24984 KB Output is correct
47 Correct 67 ms 21664 KB Output is correct
48 Correct 67 ms 20676 KB Output is correct
49 Correct 68 ms 23152 KB Output is correct
50 Correct 1684 ms 70056 KB Output is correct
51 Correct 1569 ms 76180 KB Output is correct
52 Correct 1469 ms 72600 KB Output is correct
53 Correct 1221 ms 78036 KB Output is correct
54 Correct 1603 ms 62200 KB Output is correct
55 Correct 1618 ms 64052 KB Output is correct
56 Correct 1725 ms 74752 KB Output is correct
57 Correct 1691 ms 71172 KB Output is correct
58 Correct 1602 ms 76864 KB Output is correct
59 Correct 1513 ms 72080 KB Output is correct
60 Correct 1142 ms 68964 KB Output is correct
61 Correct 1945 ms 68240 KB Output is correct
62 Correct 1835 ms 59964 KB Output is correct
63 Correct 9 ms 14412 KB Output is correct
64 Correct 8 ms 14412 KB Output is correct
65 Correct 9 ms 14428 KB Output is correct
66 Correct 30 ms 15408 KB Output is correct
67 Correct 79 ms 25648 KB Output is correct
68 Correct 70 ms 24928 KB Output is correct
69 Correct 74 ms 22824 KB Output is correct
70 Correct 10 ms 14408 KB Output is correct
71 Correct 72 ms 23052 KB Output is correct
72 Correct 78 ms 24912 KB Output is correct
73 Correct 71 ms 24740 KB Output is correct
74 Correct 71 ms 24872 KB Output is correct
75 Correct 75 ms 21456 KB Output is correct
76 Correct 63 ms 20540 KB Output is correct
77 Correct 68 ms 23096 KB Output is correct
78 Incorrect 1685 ms 67804 KB Output isn't correct
79 Halted 0 ms 0 KB -