답안 #409050

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409050 2021-05-20T06:29:33 Z Kevin_Zhang_TW From Hacks to Snitches (BOI21_watchmen) C++17
35 / 100
1880 ms 98924 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);
		}
	};

	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;
		DE(xh, uh, x, u, d);
		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:106:3: note: in expansion of macro 'DE'
  106 |   DE(x, u, d);
      |   ^~
watchmen.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
watchmen.cpp:114:3: note: in expansion of macro 'DE'
  114 |   DE(xh, uh, x, u, d);
      |   ^~
watchmen.cpp: In function 'int32_t main()':
watchmen.cpp:119:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |  for (int d = 0;d < q.size();++d) {
      |                 ~~^~~~~~~~~~
watchmen.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |   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:126:4: note: in expansion of macro 'DE'
  126 |    DE(d, x);
      |    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 15308 KB Output is correct
2 Correct 74 ms 25412 KB Output is correct
3 Correct 68 ms 24696 KB Output is correct
4 Correct 66 ms 22272 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 66 ms 23596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 15348 KB Output is correct
2 Correct 80 ms 25460 KB Output is correct
3 Correct 76 ms 24772 KB Output is correct
4 Correct 67 ms 22260 KB Output is correct
5 Correct 8 ms 14404 KB Output is correct
6 Correct 66 ms 23600 KB Output is correct
7 Correct 68 ms 24764 KB Output is correct
8 Correct 68 ms 24664 KB Output is correct
9 Correct 69 ms 24692 KB Output is correct
10 Correct 65 ms 21416 KB Output is correct
11 Correct 67 ms 20620 KB Output is correct
12 Correct 66 ms 22976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 15348 KB Output is correct
2 Correct 80 ms 25460 KB Output is correct
3 Correct 76 ms 24772 KB Output is correct
4 Correct 67 ms 22260 KB Output is correct
5 Correct 8 ms 14404 KB Output is correct
6 Correct 66 ms 23600 KB Output is correct
7 Correct 68 ms 24764 KB Output is correct
8 Correct 68 ms 24664 KB Output is correct
9 Correct 69 ms 24692 KB Output is correct
10 Correct 65 ms 21416 KB Output is correct
11 Correct 67 ms 20620 KB Output is correct
12 Correct 66 ms 22976 KB Output is correct
13 Correct 28 ms 15364 KB Output is correct
14 Correct 79 ms 25476 KB Output is correct
15 Correct 71 ms 24760 KB Output is correct
16 Correct 70 ms 22260 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 73 ms 23636 KB Output is correct
19 Correct 67 ms 24672 KB Output is correct
20 Correct 66 ms 24696 KB Output is correct
21 Correct 72 ms 24696 KB Output is correct
22 Correct 64 ms 21404 KB Output is correct
23 Correct 63 ms 20556 KB Output is correct
24 Correct 68 ms 22936 KB Output is correct
25 Correct 1692 ms 70040 KB Output is correct
26 Correct 1686 ms 75920 KB Output is correct
27 Correct 1512 ms 72352 KB Output is correct
28 Correct 1249 ms 77736 KB Output is correct
29 Correct 1605 ms 61664 KB Output is correct
30 Correct 1636 ms 63660 KB Output is correct
31 Correct 1741 ms 76912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 15348 KB Output is correct
2 Correct 80 ms 25460 KB Output is correct
3 Correct 76 ms 24772 KB Output is correct
4 Correct 67 ms 22260 KB Output is correct
5 Correct 8 ms 14404 KB Output is correct
6 Correct 66 ms 23600 KB Output is correct
7 Correct 68 ms 24764 KB Output is correct
8 Correct 68 ms 24664 KB Output is correct
9 Correct 69 ms 24692 KB Output is correct
10 Correct 65 ms 21416 KB Output is correct
11 Correct 67 ms 20620 KB Output is correct
12 Correct 66 ms 22976 KB Output is correct
13 Correct 28 ms 15364 KB Output is correct
14 Correct 79 ms 25476 KB Output is correct
15 Correct 71 ms 24760 KB Output is correct
16 Correct 70 ms 22260 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 73 ms 23636 KB Output is correct
19 Correct 67 ms 24672 KB Output is correct
20 Correct 66 ms 24696 KB Output is correct
21 Correct 72 ms 24696 KB Output is correct
22 Correct 64 ms 21404 KB Output is correct
23 Correct 63 ms 20556 KB Output is correct
24 Correct 68 ms 22936 KB Output is correct
25 Correct 1692 ms 70040 KB Output is correct
26 Correct 1686 ms 75920 KB Output is correct
27 Correct 1512 ms 72352 KB Output is correct
28 Correct 1249 ms 77736 KB Output is correct
29 Correct 1605 ms 61664 KB Output is correct
30 Correct 1636 ms 63660 KB Output is correct
31 Correct 1741 ms 76912 KB Output is correct
32 Correct 27 ms 15400 KB Output is correct
33 Correct 78 ms 25652 KB Output is correct
34 Correct 75 ms 24956 KB Output is correct
35 Correct 64 ms 22388 KB Output is correct
36 Correct 9 ms 14412 KB Output is correct
37 Correct 69 ms 23800 KB Output is correct
38 Correct 68 ms 24964 KB Output is correct
39 Correct 69 ms 24824 KB Output is correct
40 Correct 74 ms 24952 KB Output is correct
41 Correct 64 ms 21544 KB Output is correct
42 Correct 62 ms 20804 KB Output is correct
43 Correct 73 ms 23068 KB Output is correct
44 Correct 1739 ms 69916 KB Output is correct
45 Correct 1643 ms 76340 KB Output is correct
46 Correct 1510 ms 72448 KB Output is correct
47 Correct 1230 ms 77912 KB Output is correct
48 Correct 1665 ms 61684 KB Output is correct
49 Correct 1652 ms 63552 KB Output is correct
50 Correct 1792 ms 77016 KB Output is correct
51 Correct 1689 ms 70944 KB Output is correct
52 Correct 1666 ms 77048 KB Output is correct
53 Correct 1520 ms 71816 KB Output is correct
54 Correct 1181 ms 68852 KB Output is correct
55 Correct 1830 ms 67608 KB Output is correct
56 Correct 1880 ms 98924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 15308 KB Output is correct
2 Correct 74 ms 25412 KB Output is correct
3 Correct 68 ms 24696 KB Output is correct
4 Correct 66 ms 22272 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 66 ms 23596 KB Output is correct
7 Correct 27 ms 15348 KB Output is correct
8 Correct 80 ms 25460 KB Output is correct
9 Correct 76 ms 24772 KB Output is correct
10 Correct 67 ms 22260 KB Output is correct
11 Correct 8 ms 14404 KB Output is correct
12 Correct 66 ms 23600 KB Output is correct
13 Correct 68 ms 24764 KB Output is correct
14 Correct 68 ms 24664 KB Output is correct
15 Correct 69 ms 24692 KB Output is correct
16 Correct 65 ms 21416 KB Output is correct
17 Correct 67 ms 20620 KB Output is correct
18 Correct 66 ms 22976 KB Output is correct
19 Correct 9 ms 14404 KB Output is correct
20 Correct 10 ms 14308 KB Output is correct
21 Correct 12 ms 14348 KB Output is correct
22 Correct 30 ms 15892 KB Output is correct
23 Correct 91 ms 26652 KB Output is correct
24 Correct 71 ms 25848 KB Output is correct
25 Correct 74 ms 23540 KB Output is correct
26 Correct 9 ms 14408 KB Output is correct
27 Correct 74 ms 24800 KB Output is correct
28 Correct 73 ms 25848 KB Output is correct
29 Correct 80 ms 25824 KB Output is correct
30 Correct 89 ms 25844 KB Output is correct
31 Correct 77 ms 22576 KB Output is correct
32 Correct 73 ms 21752 KB Output is correct
33 Correct 74 ms 24008 KB Output is correct
34 Incorrect 1868 ms 78508 KB Output isn't correct
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 15308 KB Output is correct
2 Correct 74 ms 25412 KB Output is correct
3 Correct 68 ms 24696 KB Output is correct
4 Correct 66 ms 22272 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 66 ms 23596 KB Output is correct
7 Correct 27 ms 15348 KB Output is correct
8 Correct 80 ms 25460 KB Output is correct
9 Correct 76 ms 24772 KB Output is correct
10 Correct 67 ms 22260 KB Output is correct
11 Correct 8 ms 14404 KB Output is correct
12 Correct 66 ms 23600 KB Output is correct
13 Correct 68 ms 24764 KB Output is correct
14 Correct 68 ms 24664 KB Output is correct
15 Correct 69 ms 24692 KB Output is correct
16 Correct 65 ms 21416 KB Output is correct
17 Correct 67 ms 20620 KB Output is correct
18 Correct 66 ms 22976 KB Output is correct
19 Correct 28 ms 15364 KB Output is correct
20 Correct 79 ms 25476 KB Output is correct
21 Correct 71 ms 24760 KB Output is correct
22 Correct 70 ms 22260 KB Output is correct
23 Correct 9 ms 14412 KB Output is correct
24 Correct 73 ms 23636 KB Output is correct
25 Correct 67 ms 24672 KB Output is correct
26 Correct 66 ms 24696 KB Output is correct
27 Correct 72 ms 24696 KB Output is correct
28 Correct 64 ms 21404 KB Output is correct
29 Correct 63 ms 20556 KB Output is correct
30 Correct 68 ms 22936 KB Output is correct
31 Correct 1692 ms 70040 KB Output is correct
32 Correct 1686 ms 75920 KB Output is correct
33 Correct 1512 ms 72352 KB Output is correct
34 Correct 1249 ms 77736 KB Output is correct
35 Correct 1605 ms 61664 KB Output is correct
36 Correct 1636 ms 63660 KB Output is correct
37 Correct 1741 ms 76912 KB Output is correct
38 Correct 9 ms 14404 KB Output is correct
39 Correct 10 ms 14308 KB Output is correct
40 Correct 12 ms 14348 KB Output is correct
41 Correct 30 ms 15892 KB Output is correct
42 Correct 91 ms 26652 KB Output is correct
43 Correct 71 ms 25848 KB Output is correct
44 Correct 74 ms 23540 KB Output is correct
45 Correct 9 ms 14408 KB Output is correct
46 Correct 74 ms 24800 KB Output is correct
47 Correct 73 ms 25848 KB Output is correct
48 Correct 80 ms 25824 KB Output is correct
49 Correct 89 ms 25844 KB Output is correct
50 Correct 77 ms 22576 KB Output is correct
51 Correct 73 ms 21752 KB Output is correct
52 Correct 74 ms 24008 KB Output is correct
53 Incorrect 1868 ms 78508 KB Output isn't correct
54 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 15308 KB Output is correct
2 Correct 74 ms 25412 KB Output is correct
3 Correct 68 ms 24696 KB Output is correct
4 Correct 66 ms 22272 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 66 ms 23596 KB Output is correct
7 Correct 27 ms 15348 KB Output is correct
8 Correct 80 ms 25460 KB Output is correct
9 Correct 76 ms 24772 KB Output is correct
10 Correct 67 ms 22260 KB Output is correct
11 Correct 8 ms 14404 KB Output is correct
12 Correct 66 ms 23600 KB Output is correct
13 Correct 68 ms 24764 KB Output is correct
14 Correct 68 ms 24664 KB Output is correct
15 Correct 69 ms 24692 KB Output is correct
16 Correct 65 ms 21416 KB Output is correct
17 Correct 67 ms 20620 KB Output is correct
18 Correct 66 ms 22976 KB Output is correct
19 Correct 28 ms 15364 KB Output is correct
20 Correct 79 ms 25476 KB Output is correct
21 Correct 71 ms 24760 KB Output is correct
22 Correct 70 ms 22260 KB Output is correct
23 Correct 9 ms 14412 KB Output is correct
24 Correct 73 ms 23636 KB Output is correct
25 Correct 67 ms 24672 KB Output is correct
26 Correct 66 ms 24696 KB Output is correct
27 Correct 72 ms 24696 KB Output is correct
28 Correct 64 ms 21404 KB Output is correct
29 Correct 63 ms 20556 KB Output is correct
30 Correct 68 ms 22936 KB Output is correct
31 Correct 1692 ms 70040 KB Output is correct
32 Correct 1686 ms 75920 KB Output is correct
33 Correct 1512 ms 72352 KB Output is correct
34 Correct 1249 ms 77736 KB Output is correct
35 Correct 1605 ms 61664 KB Output is correct
36 Correct 1636 ms 63660 KB Output is correct
37 Correct 1741 ms 76912 KB Output is correct
38 Correct 27 ms 15400 KB Output is correct
39 Correct 78 ms 25652 KB Output is correct
40 Correct 75 ms 24956 KB Output is correct
41 Correct 64 ms 22388 KB Output is correct
42 Correct 9 ms 14412 KB Output is correct
43 Correct 69 ms 23800 KB Output is correct
44 Correct 68 ms 24964 KB Output is correct
45 Correct 69 ms 24824 KB Output is correct
46 Correct 74 ms 24952 KB Output is correct
47 Correct 64 ms 21544 KB Output is correct
48 Correct 62 ms 20804 KB Output is correct
49 Correct 73 ms 23068 KB Output is correct
50 Correct 1739 ms 69916 KB Output is correct
51 Correct 1643 ms 76340 KB Output is correct
52 Correct 1510 ms 72448 KB Output is correct
53 Correct 1230 ms 77912 KB Output is correct
54 Correct 1665 ms 61684 KB Output is correct
55 Correct 1652 ms 63552 KB Output is correct
56 Correct 1792 ms 77016 KB Output is correct
57 Correct 1689 ms 70944 KB Output is correct
58 Correct 1666 ms 77048 KB Output is correct
59 Correct 1520 ms 71816 KB Output is correct
60 Correct 1181 ms 68852 KB Output is correct
61 Correct 1830 ms 67608 KB Output is correct
62 Correct 1880 ms 98924 KB Output is correct
63 Correct 9 ms 14404 KB Output is correct
64 Correct 10 ms 14308 KB Output is correct
65 Correct 12 ms 14348 KB Output is correct
66 Correct 30 ms 15892 KB Output is correct
67 Correct 91 ms 26652 KB Output is correct
68 Correct 71 ms 25848 KB Output is correct
69 Correct 74 ms 23540 KB Output is correct
70 Correct 9 ms 14408 KB Output is correct
71 Correct 74 ms 24800 KB Output is correct
72 Correct 73 ms 25848 KB Output is correct
73 Correct 80 ms 25824 KB Output is correct
74 Correct 89 ms 25844 KB Output is correct
75 Correct 77 ms 22576 KB Output is correct
76 Correct 73 ms 21752 KB Output is correct
77 Correct 74 ms 24008 KB Output is correct
78 Incorrect 1868 ms 78508 KB Output isn't correct
79 Halted 0 ms 0 KB -