#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define tp3 tuple<int, int, int>
const int N = 1e5 + 1, K = 2750 + 1, inf = INT_MAX;
int n, m, k, Lcm, copId[N];
vector<int> adj[N], cop[N];
vector<vector<int>> dp;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
memset(copId, -1, sizeof(copId));
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i = 0; i < n; i++) {
adj[i].push_back(i);
}
cin >> k;
Lcm = 1;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
Lcm = min(n + K, lcm(Lcm, x));
cop[i].resize(x);
for (int& j : cop[i]) {
cin >> j;
j--;
copId[j] = i;
}
}
dp.resize(Lcm, vector<int>(n, inf));
queue<tp3> q;
q.push(make_tuple(0, 0, 0)); // {dis, sec, node}
while (!q.empty()) {
int d = get<0>(q.front());
int s = get<1>(q.front());
int u = get<2>(q.front());
q.pop();
if (!(rand() % K) && d > n) continue;
if (copId[u] != -1) {
int x = copId[u];
if (cop[x][s % cop[x].size()] == u) continue;
}
if (dp[s][u] <= d) continue;
dp[s][u] = d;
for (int& v : adj[u]) {
int nwSec = (s+1) % Lcm;
if (copId[v] != -1) {
int x = copId[v];
if (cop[x][s] == v && cop[x][(s+1) % cop[x].size()] == u) {
// no puedo ir a este nodo
}
else {
if (dp[nwSec][v] > d+1) q.push(make_tuple(d+1, nwSec, v));
}
}
else {
if (dp[nwSec][v] > d+1) q.push(make_tuple(d+1, nwSec, v));
}
}
}
int ans = inf;
for (int i = 0; i < Lcm; i++) {
ans = min(ans, dp[i][n-1]);
}
if (ans == inf) cout << "impossible\n";
else cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
451 ms |
10340 KB |
Output is correct |
2 |
Correct |
1481 ms |
57936 KB |
Output is correct |
3 |
Correct |
1512 ms |
54112 KB |
Output is correct |
4 |
Correct |
1530 ms |
54908 KB |
Output is correct |
5 |
Correct |
3 ms |
5464 KB |
Output is correct |
6 |
Correct |
1538 ms |
54276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
10332 KB |
Output is correct |
2 |
Correct |
1420 ms |
58076 KB |
Output is correct |
3 |
Correct |
1400 ms |
54040 KB |
Output is correct |
4 |
Correct |
1534 ms |
54892 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
1580 ms |
54424 KB |
Output is correct |
7 |
Runtime error |
285 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
10332 KB |
Output is correct |
2 |
Correct |
1420 ms |
58076 KB |
Output is correct |
3 |
Correct |
1400 ms |
54040 KB |
Output is correct |
4 |
Correct |
1534 ms |
54892 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
1580 ms |
54424 KB |
Output is correct |
7 |
Runtime error |
285 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
10332 KB |
Output is correct |
2 |
Correct |
1420 ms |
58076 KB |
Output is correct |
3 |
Correct |
1400 ms |
54040 KB |
Output is correct |
4 |
Correct |
1534 ms |
54892 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
1580 ms |
54424 KB |
Output is correct |
7 |
Runtime error |
285 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
451 ms |
10340 KB |
Output is correct |
2 |
Correct |
1481 ms |
57936 KB |
Output is correct |
3 |
Correct |
1512 ms |
54112 KB |
Output is correct |
4 |
Correct |
1530 ms |
54908 KB |
Output is correct |
5 |
Correct |
3 ms |
5464 KB |
Output is correct |
6 |
Correct |
1538 ms |
54276 KB |
Output is correct |
7 |
Correct |
448 ms |
10332 KB |
Output is correct |
8 |
Correct |
1420 ms |
58076 KB |
Output is correct |
9 |
Correct |
1400 ms |
54040 KB |
Output is correct |
10 |
Correct |
1534 ms |
54892 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
1580 ms |
54424 KB |
Output is correct |
13 |
Runtime error |
285 ms |
524288 KB |
Execution killed with signal 9 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
451 ms |
10340 KB |
Output is correct |
2 |
Correct |
1481 ms |
57936 KB |
Output is correct |
3 |
Correct |
1512 ms |
54112 KB |
Output is correct |
4 |
Correct |
1530 ms |
54908 KB |
Output is correct |
5 |
Correct |
3 ms |
5464 KB |
Output is correct |
6 |
Correct |
1538 ms |
54276 KB |
Output is correct |
7 |
Correct |
448 ms |
10332 KB |
Output is correct |
8 |
Correct |
1420 ms |
58076 KB |
Output is correct |
9 |
Correct |
1400 ms |
54040 KB |
Output is correct |
10 |
Correct |
1534 ms |
54892 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
1580 ms |
54424 KB |
Output is correct |
13 |
Runtime error |
285 ms |
524288 KB |
Execution killed with signal 9 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
451 ms |
10340 KB |
Output is correct |
2 |
Correct |
1481 ms |
57936 KB |
Output is correct |
3 |
Correct |
1512 ms |
54112 KB |
Output is correct |
4 |
Correct |
1530 ms |
54908 KB |
Output is correct |
5 |
Correct |
3 ms |
5464 KB |
Output is correct |
6 |
Correct |
1538 ms |
54276 KB |
Output is correct |
7 |
Correct |
448 ms |
10332 KB |
Output is correct |
8 |
Correct |
1420 ms |
58076 KB |
Output is correct |
9 |
Correct |
1400 ms |
54040 KB |
Output is correct |
10 |
Correct |
1534 ms |
54892 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
1580 ms |
54424 KB |
Output is correct |
13 |
Runtime error |
285 ms |
524288 KB |
Execution killed with signal 9 |
14 |
Halted |
0 ms |
0 KB |
- |