#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 250013;
const int INF = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, M, K;
vi edge[MAXN];
pii ban[MAXN];
map<int, pii> banedge[MAXN];
int dist[MAXN];
bool seen[MAXN][4];
priority_queue<pii, vector<pii>, greater<pii> > pq;
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
FOR(i, 0, M)
{
int u, v;
cin >> u >> v; u--; v--;
edge[u].PB(v);
edge[v].PB(u);
}
FOR(i, 0, N)
{
edge[i].PB(i);
}
cin >> K;
FOR(i, 0, K)
{
int c; cin >> c;
vi s(c);
FOR(j, 0, c)
{
cin >> s[j]; s[j]--;
ban[s[j]] = {j, c};
}
FOR(j, 0, c)
{
int u = s[j], v = s[(j == c - 1 ? 0 : j + 1)];
banedge[u][v] = {j, c};
banedge[v][u] = {j, c};
// cerr << u << ' ' << v << ' ' << j << ' ' << c << endl;
}
}
fill(dist, dist + N, INF);
dist[0] = 0;
pq.push({0, 0});
while(!pq.empty())
{
int d = pq.top().fi, u = pq.top().se; pq.pop();
if (d - dist[u] > 3) continue;
if (seen[u][d - dist[u]]) continue;
seen[u][d - dist[u]] = true;
for (int v : edge[u])
{
if (ban[v].se != 0 && (d + 1) % ban[v].se == ban[v].fi) continue;
if (banedge[u].count(v) && d % banedge[u][v].se == banedge[u][v].fi) continue;
if (dist[v] > d + 1)
{
dist[v] = d + 1;
}
// cerr << u << ' ' << v << ' ' << d + 1 << endl;
pq.push({d + 1, v});
//you can go from u -> v at time t iff:
//t != ban[v].fi mod ban[v].se
//t-1 != ban[u][v].fi mod ban[u][v].se
}
}
if (dist[N - 1] >= INF)
{
cout << "impossible\n";
return 0;
}
cout << dist[N - 1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
21064 KB |
Output is correct |
2 |
Incorrect |
138 ms |
22204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
21052 KB |
Output is correct |
2 |
Incorrect |
146 ms |
22204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
21052 KB |
Output is correct |
2 |
Incorrect |
146 ms |
22204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
21052 KB |
Output is correct |
2 |
Incorrect |
146 ms |
22204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
21064 KB |
Output is correct |
2 |
Incorrect |
138 ms |
22204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
21064 KB |
Output is correct |
2 |
Incorrect |
138 ms |
22204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
21064 KB |
Output is correct |
2 |
Incorrect |
138 ms |
22204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |