답안 #709842

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709842 2023-03-14T16:45:31 Z barcalona From Hacks to Snitches (BOI21_watchmen) C++14
70 / 100
2125 ms 159212 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 250'010;
const int L = 50;
bool has_watch[N];
int watch_size[N];
int watch_ind[N];
int watch_guard[N];

int watch_size_list[L];

int n, m, k;

int *nxt[L][L];

void calc_nxt(int step, int size)
{
	if (nxt[step][size])
		return;
	nxt[step][size] = new int[size];
	int cnt = 0;
	for (int beg=0; cnt < size; ++beg) {
		vector<pii> vec;
		int x = beg;
		do {
			while (vec.size() && vec.back().first > x)
				vec.pop_back();
			nxt[step][size][x] = vec.size()?
					cnt - vec.back().second:
					-1;
			vec.push_back({x, cnt});
			++cnt;
			x = x-step%size;
			x = x<0?x+size:x;
		} while (x != beg);
	}
}

vector<int> A[N];
int norm_dis[N];

void bfs(int s)
{
	memset(norm_dis, -1, sizeof(norm_dis));
	vector<int> q = {s};
	Loop (i,0,q.size()) {
		int v = q[i];
		for (int u : A[v]) {
			if (norm_dis[u] != -1)
				continue;
			norm_dis[u] = norm_dis[v] + 1;
			q.push_back(u);
		}
	}
}

vector<ll> dis[N];

void upS(int from, int v, ll d, set<tuple<ll,int,int>> &Set)
{
	int md = has_watch[v]? d % watch_size[v]: 0;
	if (d >= dis[v][md])
		return;
	if (has_watch[v] && md == watch_ind[v])
		return;
	if (has_watch[from] && has_watch[v] && watch_guard[from] == watch_guard[v] && watch_ind[from] == md && (watch_ind[v] == md-1 || watch_ind[v] == md+watch_size[v]-1))
		return;
	Set.erase ({dis[v][md]+norm_dis[v], md, v});
	dis[v][md] = d;
	Set.insert({dis[v][md]+norm_dis[v], md, v});
}

void up3(int v, int u, ll d, set<tuple<ll,int,int>> &Set)
{
	int step = watch_size[v];
	int size = watch_size[u];
	int x = (d + size - watch_ind[u]) % size;
	for (;;) {
		upS(v, u, d+1, Set);
		ll cnt = nxt[step][size][x];
		if (cnt == -1)
			break;
		d += cnt*step;
		x = (x + cnt*step) % size;
	}
}

ll dij(int s, int t)
{
	set<tuple<ll,int,int>> Set;
	Loop (i,0,N)
		dis[i] = vector<ll>(has_watch[i]?watch_size[i]:1, (ll)1e17);
	dis[s][0] = 0;
	Set.insert({dis[s][0]+norm_dis[s], 0, s});
	while (Set.size()) {
		auto [dard, md, v] = *Set.begin();
		Set.erase(Set.begin());
		ll d = dis[v][md];
		if (v == t)
			return d;
		upS(v, v, d+1, Set);
		for (int u : A[v]) {
			if (has_watch[u] && has_watch[v]) {
				up3(v, u, d, Set);
			} else if (has_watch[u]) {
				upS(v, u, d+1, Set);
				int wait = watch_ind[u] - d%watch_size[u];
				wait = wait<0?wait+watch_size[u]:wait;
				upS(v, u, d+1+wait, Set);
			} else {
				upS(v, u, d+1, Set);
			}
		}
	}
	return -1;
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n >> m;
	Loop (i,0,m) {
		int v, u;
		cin >> v >> u;
		--v; --u;
		A[v].push_back(u);
		A[u].push_back(v);
	}
	cin >> k;
	Loop (i,0,k) {
		int l;
		cin >> l;
		watch_size_list[i] = l;
		Loop (j,0,l) {
			int v;
			cin >> v;
			--v;
			watch_size[v] = l;
			watch_ind[v] = j;
			has_watch[v] = 1;
			watch_guard[v] = i;
		}
	}
	Loop (i,0,k) Loop (j,0,k)
		calc_nxt(watch_size_list[i], watch_size_list[j]);
	bfs(n-1);
	ll ans = dij(0, n-1);
	if (ans == -1)
		cout << "impossible\n";
	else
		cout << ans << '\n';
}

Compilation message

watchmen.cpp: In function 'll dij(int, int)':
watchmen.cpp:102:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  102 |   auto [dard, md, v] = *Set.begin();
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 22348 KB Output is correct
2 Correct 134 ms 26196 KB Output is correct
3 Correct 101 ms 25800 KB Output is correct
4 Correct 93 ms 26064 KB Output is correct
5 Correct 23 ms 20912 KB Output is correct
6 Correct 137 ms 25880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 22484 KB Output is correct
2 Correct 108 ms 26124 KB Output is correct
3 Correct 83 ms 25828 KB Output is correct
4 Correct 83 ms 26152 KB Output is correct
5 Correct 23 ms 20948 KB Output is correct
6 Correct 81 ms 25836 KB Output is correct
7 Correct 84 ms 25816 KB Output is correct
8 Correct 92 ms 25828 KB Output is correct
9 Correct 98 ms 25864 KB Output is correct
10 Correct 69 ms 26104 KB Output is correct
11 Correct 59 ms 25764 KB Output is correct
12 Correct 77 ms 25796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 22484 KB Output is correct
2 Correct 108 ms 26124 KB Output is correct
3 Correct 83 ms 25828 KB Output is correct
4 Correct 83 ms 26152 KB Output is correct
5 Correct 23 ms 20948 KB Output is correct
6 Correct 81 ms 25836 KB Output is correct
7 Correct 84 ms 25816 KB Output is correct
8 Correct 92 ms 25828 KB Output is correct
9 Correct 98 ms 25864 KB Output is correct
10 Correct 69 ms 26104 KB Output is correct
11 Correct 59 ms 25764 KB Output is correct
12 Correct 77 ms 25796 KB Output is correct
13 Correct 35 ms 22464 KB Output is correct
14 Correct 94 ms 26172 KB Output is correct
15 Correct 106 ms 25876 KB Output is correct
16 Correct 79 ms 26208 KB Output is correct
17 Correct 20 ms 20984 KB Output is correct
18 Correct 74 ms 25820 KB Output is correct
19 Correct 86 ms 25804 KB Output is correct
20 Correct 95 ms 25800 KB Output is correct
21 Correct 100 ms 25948 KB Output is correct
22 Correct 78 ms 25992 KB Output is correct
23 Correct 61 ms 25776 KB Output is correct
24 Correct 94 ms 25804 KB Output is correct
25 Correct 1728 ms 104248 KB Output is correct
26 Correct 1890 ms 108032 KB Output is correct
27 Correct 1737 ms 103272 KB Output is correct
28 Correct 1399 ms 105748 KB Output is correct
29 Correct 1775 ms 102352 KB Output is correct
30 Correct 1634 ms 102688 KB Output is correct
31 Correct 1731 ms 109152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 22484 KB Output is correct
2 Correct 108 ms 26124 KB Output is correct
3 Correct 83 ms 25828 KB Output is correct
4 Correct 83 ms 26152 KB Output is correct
5 Correct 23 ms 20948 KB Output is correct
6 Correct 81 ms 25836 KB Output is correct
7 Correct 84 ms 25816 KB Output is correct
8 Correct 92 ms 25828 KB Output is correct
9 Correct 98 ms 25864 KB Output is correct
10 Correct 69 ms 26104 KB Output is correct
11 Correct 59 ms 25764 KB Output is correct
12 Correct 77 ms 25796 KB Output is correct
13 Correct 35 ms 22464 KB Output is correct
14 Correct 94 ms 26172 KB Output is correct
15 Correct 106 ms 25876 KB Output is correct
16 Correct 79 ms 26208 KB Output is correct
17 Correct 20 ms 20984 KB Output is correct
18 Correct 74 ms 25820 KB Output is correct
19 Correct 86 ms 25804 KB Output is correct
20 Correct 95 ms 25800 KB Output is correct
21 Correct 100 ms 25948 KB Output is correct
22 Correct 78 ms 25992 KB Output is correct
23 Correct 61 ms 25776 KB Output is correct
24 Correct 94 ms 25804 KB Output is correct
25 Correct 1728 ms 104248 KB Output is correct
26 Correct 1890 ms 108032 KB Output is correct
27 Correct 1737 ms 103272 KB Output is correct
28 Correct 1399 ms 105748 KB Output is correct
29 Correct 1775 ms 102352 KB Output is correct
30 Correct 1634 ms 102688 KB Output is correct
31 Correct 1731 ms 109152 KB Output is correct
32 Correct 35 ms 22472 KB Output is correct
33 Correct 113 ms 26100 KB Output is correct
34 Correct 96 ms 25836 KB Output is correct
35 Correct 104 ms 26356 KB Output is correct
36 Correct 19 ms 20948 KB Output is correct
37 Correct 107 ms 25884 KB Output is correct
38 Correct 125 ms 25832 KB Output is correct
39 Correct 94 ms 25728 KB Output is correct
40 Correct 107 ms 25876 KB Output is correct
41 Correct 76 ms 26088 KB Output is correct
42 Correct 72 ms 25804 KB Output is correct
43 Correct 76 ms 25792 KB Output is correct
44 Correct 1734 ms 104076 KB Output is correct
45 Correct 1712 ms 107992 KB Output is correct
46 Correct 1556 ms 103224 KB Output is correct
47 Correct 1371 ms 105752 KB Output is correct
48 Correct 1716 ms 102648 KB Output is correct
49 Correct 1563 ms 102808 KB Output is correct
50 Correct 1712 ms 109108 KB Output is correct
51 Correct 1813 ms 122384 KB Output is correct
52 Correct 2125 ms 137692 KB Output is correct
53 Correct 1586 ms 122840 KB Output is correct
54 Runtime error 1004 ms 159212 KB Execution killed with signal 11
55 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 22348 KB Output is correct
2 Correct 134 ms 26196 KB Output is correct
3 Correct 101 ms 25800 KB Output is correct
4 Correct 93 ms 26064 KB Output is correct
5 Correct 23 ms 20912 KB Output is correct
6 Correct 137 ms 25880 KB Output is correct
7 Correct 32 ms 22484 KB Output is correct
8 Correct 108 ms 26124 KB Output is correct
9 Correct 83 ms 25828 KB Output is correct
10 Correct 83 ms 26152 KB Output is correct
11 Correct 23 ms 20948 KB Output is correct
12 Correct 81 ms 25836 KB Output is correct
13 Correct 84 ms 25816 KB Output is correct
14 Correct 92 ms 25828 KB Output is correct
15 Correct 98 ms 25864 KB Output is correct
16 Correct 69 ms 26104 KB Output is correct
17 Correct 59 ms 25764 KB Output is correct
18 Correct 77 ms 25796 KB Output is correct
19 Correct 19 ms 20820 KB Output is correct
20 Correct 17 ms 20784 KB Output is correct
21 Correct 19 ms 20840 KB Output is correct
22 Correct 36 ms 22420 KB Output is correct
23 Correct 98 ms 26208 KB Output is correct
24 Correct 90 ms 25880 KB Output is correct
25 Correct 71 ms 26084 KB Output is correct
26 Correct 20 ms 20912 KB Output is correct
27 Correct 88 ms 25796 KB Output is correct
28 Correct 107 ms 25792 KB Output is correct
29 Correct 119 ms 25828 KB Output is correct
30 Correct 118 ms 25912 KB Output is correct
31 Correct 73 ms 26076 KB Output is correct
32 Correct 75 ms 25848 KB Output is correct
33 Correct 88 ms 25764 KB Output is correct
34 Correct 1857 ms 103872 KB Output is correct
35 Correct 1952 ms 102084 KB Output is correct
36 Correct 1916 ms 102060 KB Output is correct
37 Correct 1788 ms 106664 KB Output is correct
38 Correct 1615 ms 103132 KB Output is correct
39 Correct 1641 ms 102640 KB Output is correct
40 Correct 1787 ms 102952 KB Output is correct
41 Correct 1626 ms 102836 KB Output is correct
42 Correct 1717 ms 103244 KB Output is correct
43 Correct 1755 ms 107248 KB Output is correct
44 Correct 1695 ms 107536 KB Output is correct
45 Correct 1930 ms 105192 KB Output is correct
46 Correct 1678 ms 102872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 22348 KB Output is correct
2 Correct 134 ms 26196 KB Output is correct
3 Correct 101 ms 25800 KB Output is correct
4 Correct 93 ms 26064 KB Output is correct
5 Correct 23 ms 20912 KB Output is correct
6 Correct 137 ms 25880 KB Output is correct
7 Correct 32 ms 22484 KB Output is correct
8 Correct 108 ms 26124 KB Output is correct
9 Correct 83 ms 25828 KB Output is correct
10 Correct 83 ms 26152 KB Output is correct
11 Correct 23 ms 20948 KB Output is correct
12 Correct 81 ms 25836 KB Output is correct
13 Correct 84 ms 25816 KB Output is correct
14 Correct 92 ms 25828 KB Output is correct
15 Correct 98 ms 25864 KB Output is correct
16 Correct 69 ms 26104 KB Output is correct
17 Correct 59 ms 25764 KB Output is correct
18 Correct 77 ms 25796 KB Output is correct
19 Correct 35 ms 22464 KB Output is correct
20 Correct 94 ms 26172 KB Output is correct
21 Correct 106 ms 25876 KB Output is correct
22 Correct 79 ms 26208 KB Output is correct
23 Correct 20 ms 20984 KB Output is correct
24 Correct 74 ms 25820 KB Output is correct
25 Correct 86 ms 25804 KB Output is correct
26 Correct 95 ms 25800 KB Output is correct
27 Correct 100 ms 25948 KB Output is correct
28 Correct 78 ms 25992 KB Output is correct
29 Correct 61 ms 25776 KB Output is correct
30 Correct 94 ms 25804 KB Output is correct
31 Correct 1728 ms 104248 KB Output is correct
32 Correct 1890 ms 108032 KB Output is correct
33 Correct 1737 ms 103272 KB Output is correct
34 Correct 1399 ms 105748 KB Output is correct
35 Correct 1775 ms 102352 KB Output is correct
36 Correct 1634 ms 102688 KB Output is correct
37 Correct 1731 ms 109152 KB Output is correct
38 Correct 19 ms 20820 KB Output is correct
39 Correct 17 ms 20784 KB Output is correct
40 Correct 19 ms 20840 KB Output is correct
41 Correct 36 ms 22420 KB Output is correct
42 Correct 98 ms 26208 KB Output is correct
43 Correct 90 ms 25880 KB Output is correct
44 Correct 71 ms 26084 KB Output is correct
45 Correct 20 ms 20912 KB Output is correct
46 Correct 88 ms 25796 KB Output is correct
47 Correct 107 ms 25792 KB Output is correct
48 Correct 119 ms 25828 KB Output is correct
49 Correct 118 ms 25912 KB Output is correct
50 Correct 73 ms 26076 KB Output is correct
51 Correct 75 ms 25848 KB Output is correct
52 Correct 88 ms 25764 KB Output is correct
53 Correct 1857 ms 103872 KB Output is correct
54 Correct 1952 ms 102084 KB Output is correct
55 Correct 1916 ms 102060 KB Output is correct
56 Correct 1788 ms 106664 KB Output is correct
57 Correct 1615 ms 103132 KB Output is correct
58 Correct 1641 ms 102640 KB Output is correct
59 Correct 1787 ms 102952 KB Output is correct
60 Correct 1626 ms 102836 KB Output is correct
61 Correct 1717 ms 103244 KB Output is correct
62 Correct 1755 ms 107248 KB Output is correct
63 Correct 1695 ms 107536 KB Output is correct
64 Correct 1930 ms 105192 KB Output is correct
65 Correct 1678 ms 102872 KB Output is correct
66 Correct 18 ms 20820 KB Output is correct
67 Correct 18 ms 20764 KB Output is correct
68 Correct 18 ms 20804 KB Output is correct
69 Correct 35 ms 22552 KB Output is correct
70 Correct 104 ms 26208 KB Output is correct
71 Correct 94 ms 25832 KB Output is correct
72 Correct 83 ms 26172 KB Output is correct
73 Correct 20 ms 20920 KB Output is correct
74 Correct 84 ms 25864 KB Output is correct
75 Correct 86 ms 25824 KB Output is correct
76 Correct 103 ms 25820 KB Output is correct
77 Correct 96 ms 25964 KB Output is correct
78 Correct 75 ms 26060 KB Output is correct
79 Correct 66 ms 25800 KB Output is correct
80 Correct 100 ms 25732 KB Output is correct
81 Correct 1734 ms 103728 KB Output is correct
82 Correct 1721 ms 107652 KB Output is correct
83 Correct 1502 ms 102920 KB Output is correct
84 Correct 1270 ms 105856 KB Output is correct
85 Correct 1607 ms 102428 KB Output is correct
86 Correct 1459 ms 102456 KB Output is correct
87 Correct 1686 ms 109084 KB Output is correct
88 Correct 1858 ms 103516 KB Output is correct
89 Correct 1994 ms 102148 KB Output is correct
90 Correct 1924 ms 101980 KB Output is correct
91 Correct 1731 ms 106596 KB Output is correct
92 Correct 1704 ms 103328 KB Output is correct
93 Correct 1452 ms 102636 KB Output is correct
94 Correct 1706 ms 102916 KB Output is correct
95 Correct 1546 ms 102924 KB Output is correct
96 Correct 1693 ms 103288 KB Output is correct
97 Correct 1657 ms 107580 KB Output is correct
98 Correct 1775 ms 107504 KB Output is correct
99 Correct 1862 ms 104992 KB Output is correct
100 Correct 1645 ms 102620 KB Output is correct
101 Correct 1977 ms 104760 KB Output is correct
102 Correct 1860 ms 107488 KB Output is correct
103 Correct 1658 ms 104312 KB Output is correct
104 Correct 1664 ms 106620 KB Output is correct
105 Correct 1660 ms 104200 KB Output is correct
106 Correct 1804 ms 106232 KB Output is correct
107 Correct 1618 ms 103964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 22348 KB Output is correct
2 Correct 134 ms 26196 KB Output is correct
3 Correct 101 ms 25800 KB Output is correct
4 Correct 93 ms 26064 KB Output is correct
5 Correct 23 ms 20912 KB Output is correct
6 Correct 137 ms 25880 KB Output is correct
7 Correct 32 ms 22484 KB Output is correct
8 Correct 108 ms 26124 KB Output is correct
9 Correct 83 ms 25828 KB Output is correct
10 Correct 83 ms 26152 KB Output is correct
11 Correct 23 ms 20948 KB Output is correct
12 Correct 81 ms 25836 KB Output is correct
13 Correct 84 ms 25816 KB Output is correct
14 Correct 92 ms 25828 KB Output is correct
15 Correct 98 ms 25864 KB Output is correct
16 Correct 69 ms 26104 KB Output is correct
17 Correct 59 ms 25764 KB Output is correct
18 Correct 77 ms 25796 KB Output is correct
19 Correct 35 ms 22464 KB Output is correct
20 Correct 94 ms 26172 KB Output is correct
21 Correct 106 ms 25876 KB Output is correct
22 Correct 79 ms 26208 KB Output is correct
23 Correct 20 ms 20984 KB Output is correct
24 Correct 74 ms 25820 KB Output is correct
25 Correct 86 ms 25804 KB Output is correct
26 Correct 95 ms 25800 KB Output is correct
27 Correct 100 ms 25948 KB Output is correct
28 Correct 78 ms 25992 KB Output is correct
29 Correct 61 ms 25776 KB Output is correct
30 Correct 94 ms 25804 KB Output is correct
31 Correct 1728 ms 104248 KB Output is correct
32 Correct 1890 ms 108032 KB Output is correct
33 Correct 1737 ms 103272 KB Output is correct
34 Correct 1399 ms 105748 KB Output is correct
35 Correct 1775 ms 102352 KB Output is correct
36 Correct 1634 ms 102688 KB Output is correct
37 Correct 1731 ms 109152 KB Output is correct
38 Correct 35 ms 22472 KB Output is correct
39 Correct 113 ms 26100 KB Output is correct
40 Correct 96 ms 25836 KB Output is correct
41 Correct 104 ms 26356 KB Output is correct
42 Correct 19 ms 20948 KB Output is correct
43 Correct 107 ms 25884 KB Output is correct
44 Correct 125 ms 25832 KB Output is correct
45 Correct 94 ms 25728 KB Output is correct
46 Correct 107 ms 25876 KB Output is correct
47 Correct 76 ms 26088 KB Output is correct
48 Correct 72 ms 25804 KB Output is correct
49 Correct 76 ms 25792 KB Output is correct
50 Correct 1734 ms 104076 KB Output is correct
51 Correct 1712 ms 107992 KB Output is correct
52 Correct 1556 ms 103224 KB Output is correct
53 Correct 1371 ms 105752 KB Output is correct
54 Correct 1716 ms 102648 KB Output is correct
55 Correct 1563 ms 102808 KB Output is correct
56 Correct 1712 ms 109108 KB Output is correct
57 Correct 1813 ms 122384 KB Output is correct
58 Correct 2125 ms 137692 KB Output is correct
59 Correct 1586 ms 122840 KB Output is correct
60 Runtime error 1004 ms 159212 KB Execution killed with signal 11
61 Halted 0 ms 0 KB -