답안 #409036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409036 2021-05-20T05:45:42 Z Kevin_Zhang_TW From Hacks to Snitches (BOI21_watchmen) C++17
25 / 100
6000 ms 150004 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];
vector<ll> dp[MAX_N];
int cid[MAX_N], csz[MAX_N], cpos[MAX_N];

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	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].resize(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++;
			dp[u].resize(len, inf);
			DE(u, cpos[u], len);
		}
	}

	//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);
	};

	auto upd = [&](int x, ll d) {
		if (chmin(dp[x][d % csz[x]], d)) 
			putin(d, x);
	};

	upd(1, 0);


	// shortest path

	// now there are no two cycle being connected
	// check if at x, time is d can move to 
	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] == 0) return true;
		assert(cid[x] == cid[u]);
		if (cpos[u] == (cpos[x] - 1 + csz[x]) % csz[u]) {
			if (d % csz[u] == cpos[x]) return false;
		}
		return true;
	};

	for (int d = 0;d < q.size();++d) {
		for (int i = 0;i < q[d].size();++i) {
			int x = q[d][i];
//
//	while (q.size()) {
//		//auto [d, x] = q.front(); q.pop();
//		auto [d, x] = q.top(); q.pop();
		//if (d != dp[x][ d % csz[x] ]) continue;
			if (cid[x]) {
				int sz = 0;
				for (int u : edge[x]) {
					if (cid[u] == 0) {
						upd(u, d + 1);
						continue;
					}
					else {
						if (valid(x, d + 1, u))
							upd(u, d + 1);
						edge[x][sz++] = u;
					}
				}
				edge[x].resize(sz);
				if (valid(x, d + 1, x))
					upd(x, d + 1);
			}
			else {
				int mxc = hol;
				for (int u : edge[x]) {
					if (cid[u] == 0) {
						upd(u, d + 1);
						continue;
					}
					else {
						if (valid(x, d + 1, u))
							upd(u, d + 1);

						ll t = d + 1, m = t % csz[u];
						if (m != cpos[u]) {
							ll f = cpos[u] - m;
							if (f < 0) f += csz[u];
							t += f;
						}
						++t;
						if (valid(x, t, u))
							upd(u, t);
					}
				}
			}
		}
		vector<int>().swap(q[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 function 'int32_t main()':
watchmen.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
watchmen.cpp:52:4: note: in expansion of macro 'DE'
   52 |    DE(u, cpos[u], len);
      |    ^~
watchmen.cpp: In lambda function:
watchmen.cpp:61:16: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   61 |   if (q.size() < d + 1) q.resize(d + 1);
      |       ~~~~~~~~~^~~~~~~
watchmen.cpp: In function 'int32_t main()':
watchmen.cpp:88:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |  for (int d = 0;d < q.size();++d) {
      |                 ~~^~~~~~~~~~
watchmen.cpp:89:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |   for (int i = 0;i < q[d].size();++i) {
      |                  ~~^~~~~~~~~~~~~
watchmen.cpp:114:9: warning: unused variable 'mxc' [-Wunused-variable]
  114 |     int mxc = hol;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 22396 KB Output is correct
2 Correct 109 ms 31932 KB Output is correct
3 Correct 115 ms 31264 KB Output is correct
4 Correct 114 ms 29424 KB Output is correct
5 Correct 13 ms 21556 KB Output is correct
6 Correct 84 ms 30612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 22452 KB Output is correct
2 Correct 111 ms 31992 KB Output is correct
3 Correct 90 ms 31248 KB Output is correct
4 Correct 108 ms 29412 KB Output is correct
5 Correct 13 ms 21564 KB Output is correct
6 Correct 93 ms 30560 KB Output is correct
7 Correct 92 ms 31200 KB Output is correct
8 Correct 90 ms 31228 KB Output is correct
9 Correct 91 ms 31164 KB Output is correct
10 Correct 88 ms 28932 KB Output is correct
11 Correct 84 ms 28708 KB Output is correct
12 Correct 96 ms 29644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 22452 KB Output is correct
2 Correct 111 ms 31992 KB Output is correct
3 Correct 90 ms 31248 KB Output is correct
4 Correct 108 ms 29412 KB Output is correct
5 Correct 13 ms 21564 KB Output is correct
6 Correct 93 ms 30560 KB Output is correct
7 Correct 92 ms 31200 KB Output is correct
8 Correct 90 ms 31228 KB Output is correct
9 Correct 91 ms 31164 KB Output is correct
10 Correct 88 ms 28932 KB Output is correct
11 Correct 84 ms 28708 KB Output is correct
12 Correct 96 ms 29644 KB Output is correct
13 Correct 54 ms 22376 KB Output is correct
14 Correct 102 ms 31972 KB Output is correct
15 Correct 86 ms 31292 KB Output is correct
16 Correct 105 ms 29416 KB Output is correct
17 Correct 14 ms 21600 KB Output is correct
18 Correct 93 ms 30624 KB Output is correct
19 Correct 88 ms 31292 KB Output is correct
20 Correct 89 ms 31236 KB Output is correct
21 Correct 93 ms 31168 KB Output is correct
22 Correct 91 ms 28996 KB Output is correct
23 Correct 92 ms 28620 KB Output is correct
24 Correct 84 ms 29664 KB Output is correct
25 Correct 1856 ms 77308 KB Output is correct
26 Correct 1861 ms 81664 KB Output is correct
27 Correct 1795 ms 77736 KB Output is correct
28 Correct 1337 ms 81824 KB Output is correct
29 Correct 1791 ms 69628 KB Output is correct
30 Correct 1830 ms 81416 KB Output is correct
31 Correct 1974 ms 120540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 22452 KB Output is correct
2 Correct 111 ms 31992 KB Output is correct
3 Correct 90 ms 31248 KB Output is correct
4 Correct 108 ms 29412 KB Output is correct
5 Correct 13 ms 21564 KB Output is correct
6 Correct 93 ms 30560 KB Output is correct
7 Correct 92 ms 31200 KB Output is correct
8 Correct 90 ms 31228 KB Output is correct
9 Correct 91 ms 31164 KB Output is correct
10 Correct 88 ms 28932 KB Output is correct
11 Correct 84 ms 28708 KB Output is correct
12 Correct 96 ms 29644 KB Output is correct
13 Correct 54 ms 22376 KB Output is correct
14 Correct 102 ms 31972 KB Output is correct
15 Correct 86 ms 31292 KB Output is correct
16 Correct 105 ms 29416 KB Output is correct
17 Correct 14 ms 21600 KB Output is correct
18 Correct 93 ms 30624 KB Output is correct
19 Correct 88 ms 31292 KB Output is correct
20 Correct 89 ms 31236 KB Output is correct
21 Correct 93 ms 31168 KB Output is correct
22 Correct 91 ms 28996 KB Output is correct
23 Correct 92 ms 28620 KB Output is correct
24 Correct 84 ms 29664 KB Output is correct
25 Correct 1856 ms 77308 KB Output is correct
26 Correct 1861 ms 81664 KB Output is correct
27 Correct 1795 ms 77736 KB Output is correct
28 Correct 1337 ms 81824 KB Output is correct
29 Correct 1791 ms 69628 KB Output is correct
30 Correct 1830 ms 81416 KB Output is correct
31 Correct 1974 ms 120540 KB Output is correct
32 Correct 52 ms 23004 KB Output is correct
33 Correct 103 ms 33128 KB Output is correct
34 Correct 91 ms 32440 KB Output is correct
35 Correct 109 ms 30616 KB Output is correct
36 Correct 15 ms 21588 KB Output is correct
37 Correct 89 ms 31768 KB Output is correct
38 Correct 94 ms 32440 KB Output is correct
39 Correct 93 ms 32332 KB Output is correct
40 Correct 95 ms 32408 KB Output is correct
41 Correct 88 ms 30088 KB Output is correct
42 Correct 96 ms 29768 KB Output is correct
43 Correct 86 ms 30740 KB Output is correct
44 Correct 1930 ms 115952 KB Output is correct
45 Correct 1839 ms 94592 KB Output is correct
46 Correct 1625 ms 90252 KB Output is correct
47 Correct 1344 ms 94212 KB Output is correct
48 Correct 1720 ms 82048 KB Output is correct
49 Correct 1768 ms 84684 KB Output is correct
50 Correct 1901 ms 94584 KB Output is correct
51 Correct 2178 ms 108004 KB Output is correct
52 Correct 2329 ms 150004 KB Output is correct
53 Correct 2085 ms 134596 KB Output is correct
54 Correct 1295 ms 113204 KB Output is correct
55 Execution timed out 6009 ms 109196 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 22396 KB Output is correct
2 Correct 109 ms 31932 KB Output is correct
3 Correct 115 ms 31264 KB Output is correct
4 Correct 114 ms 29424 KB Output is correct
5 Correct 13 ms 21556 KB Output is correct
6 Correct 84 ms 30612 KB Output is correct
7 Correct 51 ms 22452 KB Output is correct
8 Correct 111 ms 31992 KB Output is correct
9 Correct 90 ms 31248 KB Output is correct
10 Correct 108 ms 29412 KB Output is correct
11 Correct 13 ms 21564 KB Output is correct
12 Correct 93 ms 30560 KB Output is correct
13 Correct 92 ms 31200 KB Output is correct
14 Correct 90 ms 31228 KB Output is correct
15 Correct 91 ms 31164 KB Output is correct
16 Correct 88 ms 28932 KB Output is correct
17 Correct 84 ms 28708 KB Output is correct
18 Correct 96 ms 29644 KB Output is correct
19 Correct 15 ms 21348 KB Output is correct
20 Correct 14 ms 21444 KB Output is correct
21 Runtime error 37 ms 43256 KB Execution killed with signal 6
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 22396 KB Output is correct
2 Correct 109 ms 31932 KB Output is correct
3 Correct 115 ms 31264 KB Output is correct
4 Correct 114 ms 29424 KB Output is correct
5 Correct 13 ms 21556 KB Output is correct
6 Correct 84 ms 30612 KB Output is correct
7 Correct 51 ms 22452 KB Output is correct
8 Correct 111 ms 31992 KB Output is correct
9 Correct 90 ms 31248 KB Output is correct
10 Correct 108 ms 29412 KB Output is correct
11 Correct 13 ms 21564 KB Output is correct
12 Correct 93 ms 30560 KB Output is correct
13 Correct 92 ms 31200 KB Output is correct
14 Correct 90 ms 31228 KB Output is correct
15 Correct 91 ms 31164 KB Output is correct
16 Correct 88 ms 28932 KB Output is correct
17 Correct 84 ms 28708 KB Output is correct
18 Correct 96 ms 29644 KB Output is correct
19 Correct 54 ms 22376 KB Output is correct
20 Correct 102 ms 31972 KB Output is correct
21 Correct 86 ms 31292 KB Output is correct
22 Correct 105 ms 29416 KB Output is correct
23 Correct 14 ms 21600 KB Output is correct
24 Correct 93 ms 30624 KB Output is correct
25 Correct 88 ms 31292 KB Output is correct
26 Correct 89 ms 31236 KB Output is correct
27 Correct 93 ms 31168 KB Output is correct
28 Correct 91 ms 28996 KB Output is correct
29 Correct 92 ms 28620 KB Output is correct
30 Correct 84 ms 29664 KB Output is correct
31 Correct 1856 ms 77308 KB Output is correct
32 Correct 1861 ms 81664 KB Output is correct
33 Correct 1795 ms 77736 KB Output is correct
34 Correct 1337 ms 81824 KB Output is correct
35 Correct 1791 ms 69628 KB Output is correct
36 Correct 1830 ms 81416 KB Output is correct
37 Correct 1974 ms 120540 KB Output is correct
38 Correct 15 ms 21348 KB Output is correct
39 Correct 14 ms 21444 KB Output is correct
40 Runtime error 37 ms 43256 KB Execution killed with signal 6
41 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 22396 KB Output is correct
2 Correct 109 ms 31932 KB Output is correct
3 Correct 115 ms 31264 KB Output is correct
4 Correct 114 ms 29424 KB Output is correct
5 Correct 13 ms 21556 KB Output is correct
6 Correct 84 ms 30612 KB Output is correct
7 Correct 51 ms 22452 KB Output is correct
8 Correct 111 ms 31992 KB Output is correct
9 Correct 90 ms 31248 KB Output is correct
10 Correct 108 ms 29412 KB Output is correct
11 Correct 13 ms 21564 KB Output is correct
12 Correct 93 ms 30560 KB Output is correct
13 Correct 92 ms 31200 KB Output is correct
14 Correct 90 ms 31228 KB Output is correct
15 Correct 91 ms 31164 KB Output is correct
16 Correct 88 ms 28932 KB Output is correct
17 Correct 84 ms 28708 KB Output is correct
18 Correct 96 ms 29644 KB Output is correct
19 Correct 54 ms 22376 KB Output is correct
20 Correct 102 ms 31972 KB Output is correct
21 Correct 86 ms 31292 KB Output is correct
22 Correct 105 ms 29416 KB Output is correct
23 Correct 14 ms 21600 KB Output is correct
24 Correct 93 ms 30624 KB Output is correct
25 Correct 88 ms 31292 KB Output is correct
26 Correct 89 ms 31236 KB Output is correct
27 Correct 93 ms 31168 KB Output is correct
28 Correct 91 ms 28996 KB Output is correct
29 Correct 92 ms 28620 KB Output is correct
30 Correct 84 ms 29664 KB Output is correct
31 Correct 1856 ms 77308 KB Output is correct
32 Correct 1861 ms 81664 KB Output is correct
33 Correct 1795 ms 77736 KB Output is correct
34 Correct 1337 ms 81824 KB Output is correct
35 Correct 1791 ms 69628 KB Output is correct
36 Correct 1830 ms 81416 KB Output is correct
37 Correct 1974 ms 120540 KB Output is correct
38 Correct 52 ms 23004 KB Output is correct
39 Correct 103 ms 33128 KB Output is correct
40 Correct 91 ms 32440 KB Output is correct
41 Correct 109 ms 30616 KB Output is correct
42 Correct 15 ms 21588 KB Output is correct
43 Correct 89 ms 31768 KB Output is correct
44 Correct 94 ms 32440 KB Output is correct
45 Correct 93 ms 32332 KB Output is correct
46 Correct 95 ms 32408 KB Output is correct
47 Correct 88 ms 30088 KB Output is correct
48 Correct 96 ms 29768 KB Output is correct
49 Correct 86 ms 30740 KB Output is correct
50 Correct 1930 ms 115952 KB Output is correct
51 Correct 1839 ms 94592 KB Output is correct
52 Correct 1625 ms 90252 KB Output is correct
53 Correct 1344 ms 94212 KB Output is correct
54 Correct 1720 ms 82048 KB Output is correct
55 Correct 1768 ms 84684 KB Output is correct
56 Correct 1901 ms 94584 KB Output is correct
57 Correct 2178 ms 108004 KB Output is correct
58 Correct 2329 ms 150004 KB Output is correct
59 Correct 2085 ms 134596 KB Output is correct
60 Correct 1295 ms 113204 KB Output is correct
61 Execution timed out 6009 ms 109196 KB Time limit exceeded
62 Halted 0 ms 0 KB -