#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;
auto upd = [&](int x, ll d) {
DE(x, d);
if (chmin(dp[x][d % csz[x]], d))
q.emplace(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;
};
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]) {
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);
}
}
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);
// for (int i = 1;i <= csz[u];++i)
// if (valid(x, d + i, u))
// upd(u, d + i);
}
}
}
}
//
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:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
watchmen.cpp:60:3: note: in expansion of macro 'DE'
60 | DE(x, d);
| ^~
watchmen.cpp: In function 'int32_t main()':
watchmen.cpp:102:8: warning: unused variable 'mxc' [-Wunused-variable]
102 | int mxc = hol;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
22420 KB |
Output is correct |
2 |
Correct |
100 ms |
28732 KB |
Output is correct |
3 |
Correct |
87 ms |
28232 KB |
Output is correct |
4 |
Correct |
120 ms |
28460 KB |
Output is correct |
5 |
Correct |
20 ms |
21592 KB |
Output is correct |
6 |
Correct |
97 ms |
28248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
22348 KB |
Output is correct |
2 |
Correct |
91 ms |
28716 KB |
Output is correct |
3 |
Correct |
85 ms |
28300 KB |
Output is correct |
4 |
Correct |
120 ms |
28372 KB |
Output is correct |
5 |
Correct |
15 ms |
21580 KB |
Output is correct |
6 |
Correct |
111 ms |
28264 KB |
Output is correct |
7 |
Correct |
81 ms |
28196 KB |
Output is correct |
8 |
Correct |
98 ms |
28184 KB |
Output is correct |
9 |
Correct |
82 ms |
28136 KB |
Output is correct |
10 |
Correct |
103 ms |
28500 KB |
Output is correct |
11 |
Correct |
95 ms |
28228 KB |
Output is correct |
12 |
Correct |
82 ms |
28228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
22348 KB |
Output is correct |
2 |
Correct |
91 ms |
28716 KB |
Output is correct |
3 |
Correct |
85 ms |
28300 KB |
Output is correct |
4 |
Correct |
120 ms |
28372 KB |
Output is correct |
5 |
Correct |
15 ms |
21580 KB |
Output is correct |
6 |
Correct |
111 ms |
28264 KB |
Output is correct |
7 |
Correct |
81 ms |
28196 KB |
Output is correct |
8 |
Correct |
98 ms |
28184 KB |
Output is correct |
9 |
Correct |
82 ms |
28136 KB |
Output is correct |
10 |
Correct |
103 ms |
28500 KB |
Output is correct |
11 |
Correct |
95 ms |
28228 KB |
Output is correct |
12 |
Correct |
82 ms |
28228 KB |
Output is correct |
13 |
Correct |
85 ms |
22448 KB |
Output is correct |
14 |
Correct |
87 ms |
28772 KB |
Output is correct |
15 |
Correct |
85 ms |
28376 KB |
Output is correct |
16 |
Correct |
124 ms |
28420 KB |
Output is correct |
17 |
Correct |
15 ms |
21580 KB |
Output is correct |
18 |
Correct |
85 ms |
28316 KB |
Output is correct |
19 |
Correct |
83 ms |
28320 KB |
Output is correct |
20 |
Correct |
82 ms |
28180 KB |
Output is correct |
21 |
Correct |
82 ms |
28100 KB |
Output is correct |
22 |
Correct |
126 ms |
28584 KB |
Output is correct |
23 |
Correct |
115 ms |
28232 KB |
Output is correct |
24 |
Correct |
93 ms |
28228 KB |
Output is correct |
25 |
Correct |
1857 ms |
71856 KB |
Output is correct |
26 |
Correct |
1970 ms |
76540 KB |
Output is correct |
27 |
Correct |
1690 ms |
71992 KB |
Output is correct |
28 |
Correct |
1311 ms |
76024 KB |
Output is correct |
29 |
Execution timed out |
6014 ms |
67672 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
22348 KB |
Output is correct |
2 |
Correct |
91 ms |
28716 KB |
Output is correct |
3 |
Correct |
85 ms |
28300 KB |
Output is correct |
4 |
Correct |
120 ms |
28372 KB |
Output is correct |
5 |
Correct |
15 ms |
21580 KB |
Output is correct |
6 |
Correct |
111 ms |
28264 KB |
Output is correct |
7 |
Correct |
81 ms |
28196 KB |
Output is correct |
8 |
Correct |
98 ms |
28184 KB |
Output is correct |
9 |
Correct |
82 ms |
28136 KB |
Output is correct |
10 |
Correct |
103 ms |
28500 KB |
Output is correct |
11 |
Correct |
95 ms |
28228 KB |
Output is correct |
12 |
Correct |
82 ms |
28228 KB |
Output is correct |
13 |
Correct |
85 ms |
22448 KB |
Output is correct |
14 |
Correct |
87 ms |
28772 KB |
Output is correct |
15 |
Correct |
85 ms |
28376 KB |
Output is correct |
16 |
Correct |
124 ms |
28420 KB |
Output is correct |
17 |
Correct |
15 ms |
21580 KB |
Output is correct |
18 |
Correct |
85 ms |
28316 KB |
Output is correct |
19 |
Correct |
83 ms |
28320 KB |
Output is correct |
20 |
Correct |
82 ms |
28180 KB |
Output is correct |
21 |
Correct |
82 ms |
28100 KB |
Output is correct |
22 |
Correct |
126 ms |
28584 KB |
Output is correct |
23 |
Correct |
115 ms |
28232 KB |
Output is correct |
24 |
Correct |
93 ms |
28228 KB |
Output is correct |
25 |
Correct |
1857 ms |
71856 KB |
Output is correct |
26 |
Correct |
1970 ms |
76540 KB |
Output is correct |
27 |
Correct |
1690 ms |
71992 KB |
Output is correct |
28 |
Correct |
1311 ms |
76024 KB |
Output is correct |
29 |
Execution timed out |
6014 ms |
67672 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
22420 KB |
Output is correct |
2 |
Correct |
100 ms |
28732 KB |
Output is correct |
3 |
Correct |
87 ms |
28232 KB |
Output is correct |
4 |
Correct |
120 ms |
28460 KB |
Output is correct |
5 |
Correct |
20 ms |
21592 KB |
Output is correct |
6 |
Correct |
97 ms |
28248 KB |
Output is correct |
7 |
Correct |
81 ms |
22348 KB |
Output is correct |
8 |
Correct |
91 ms |
28716 KB |
Output is correct |
9 |
Correct |
85 ms |
28300 KB |
Output is correct |
10 |
Correct |
120 ms |
28372 KB |
Output is correct |
11 |
Correct |
15 ms |
21580 KB |
Output is correct |
12 |
Correct |
111 ms |
28264 KB |
Output is correct |
13 |
Correct |
81 ms |
28196 KB |
Output is correct |
14 |
Correct |
98 ms |
28184 KB |
Output is correct |
15 |
Correct |
82 ms |
28136 KB |
Output is correct |
16 |
Correct |
103 ms |
28500 KB |
Output is correct |
17 |
Correct |
95 ms |
28228 KB |
Output is correct |
18 |
Correct |
82 ms |
28228 KB |
Output is correct |
19 |
Correct |
13 ms |
21452 KB |
Output is correct |
20 |
Correct |
13 ms |
21464 KB |
Output is correct |
21 |
Runtime error |
35 ms |
43340 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
22420 KB |
Output is correct |
2 |
Correct |
100 ms |
28732 KB |
Output is correct |
3 |
Correct |
87 ms |
28232 KB |
Output is correct |
4 |
Correct |
120 ms |
28460 KB |
Output is correct |
5 |
Correct |
20 ms |
21592 KB |
Output is correct |
6 |
Correct |
97 ms |
28248 KB |
Output is correct |
7 |
Correct |
81 ms |
22348 KB |
Output is correct |
8 |
Correct |
91 ms |
28716 KB |
Output is correct |
9 |
Correct |
85 ms |
28300 KB |
Output is correct |
10 |
Correct |
120 ms |
28372 KB |
Output is correct |
11 |
Correct |
15 ms |
21580 KB |
Output is correct |
12 |
Correct |
111 ms |
28264 KB |
Output is correct |
13 |
Correct |
81 ms |
28196 KB |
Output is correct |
14 |
Correct |
98 ms |
28184 KB |
Output is correct |
15 |
Correct |
82 ms |
28136 KB |
Output is correct |
16 |
Correct |
103 ms |
28500 KB |
Output is correct |
17 |
Correct |
95 ms |
28228 KB |
Output is correct |
18 |
Correct |
82 ms |
28228 KB |
Output is correct |
19 |
Correct |
85 ms |
22448 KB |
Output is correct |
20 |
Correct |
87 ms |
28772 KB |
Output is correct |
21 |
Correct |
85 ms |
28376 KB |
Output is correct |
22 |
Correct |
124 ms |
28420 KB |
Output is correct |
23 |
Correct |
15 ms |
21580 KB |
Output is correct |
24 |
Correct |
85 ms |
28316 KB |
Output is correct |
25 |
Correct |
83 ms |
28320 KB |
Output is correct |
26 |
Correct |
82 ms |
28180 KB |
Output is correct |
27 |
Correct |
82 ms |
28100 KB |
Output is correct |
28 |
Correct |
126 ms |
28584 KB |
Output is correct |
29 |
Correct |
115 ms |
28232 KB |
Output is correct |
30 |
Correct |
93 ms |
28228 KB |
Output is correct |
31 |
Correct |
1857 ms |
71856 KB |
Output is correct |
32 |
Correct |
1970 ms |
76540 KB |
Output is correct |
33 |
Correct |
1690 ms |
71992 KB |
Output is correct |
34 |
Correct |
1311 ms |
76024 KB |
Output is correct |
35 |
Execution timed out |
6014 ms |
67672 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
22420 KB |
Output is correct |
2 |
Correct |
100 ms |
28732 KB |
Output is correct |
3 |
Correct |
87 ms |
28232 KB |
Output is correct |
4 |
Correct |
120 ms |
28460 KB |
Output is correct |
5 |
Correct |
20 ms |
21592 KB |
Output is correct |
6 |
Correct |
97 ms |
28248 KB |
Output is correct |
7 |
Correct |
81 ms |
22348 KB |
Output is correct |
8 |
Correct |
91 ms |
28716 KB |
Output is correct |
9 |
Correct |
85 ms |
28300 KB |
Output is correct |
10 |
Correct |
120 ms |
28372 KB |
Output is correct |
11 |
Correct |
15 ms |
21580 KB |
Output is correct |
12 |
Correct |
111 ms |
28264 KB |
Output is correct |
13 |
Correct |
81 ms |
28196 KB |
Output is correct |
14 |
Correct |
98 ms |
28184 KB |
Output is correct |
15 |
Correct |
82 ms |
28136 KB |
Output is correct |
16 |
Correct |
103 ms |
28500 KB |
Output is correct |
17 |
Correct |
95 ms |
28228 KB |
Output is correct |
18 |
Correct |
82 ms |
28228 KB |
Output is correct |
19 |
Correct |
85 ms |
22448 KB |
Output is correct |
20 |
Correct |
87 ms |
28772 KB |
Output is correct |
21 |
Correct |
85 ms |
28376 KB |
Output is correct |
22 |
Correct |
124 ms |
28420 KB |
Output is correct |
23 |
Correct |
15 ms |
21580 KB |
Output is correct |
24 |
Correct |
85 ms |
28316 KB |
Output is correct |
25 |
Correct |
83 ms |
28320 KB |
Output is correct |
26 |
Correct |
82 ms |
28180 KB |
Output is correct |
27 |
Correct |
82 ms |
28100 KB |
Output is correct |
28 |
Correct |
126 ms |
28584 KB |
Output is correct |
29 |
Correct |
115 ms |
28232 KB |
Output is correct |
30 |
Correct |
93 ms |
28228 KB |
Output is correct |
31 |
Correct |
1857 ms |
71856 KB |
Output is correct |
32 |
Correct |
1970 ms |
76540 KB |
Output is correct |
33 |
Correct |
1690 ms |
71992 KB |
Output is correct |
34 |
Correct |
1311 ms |
76024 KB |
Output is correct |
35 |
Execution timed out |
6014 ms |
67672 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |