//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <set>
//#include <ext/pb_ds/assoc_contatiner.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
void debug() {cout << endl;}
template<class T, class ... U> void debug(T a, U ... b) { cout << a << " ";debug(b...);}
template<class T> void pary (T l, T r) {
while (l != r) cout << (*l) << " ", l++;
cout << endl;
}
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const ll inf = 1LL<<60;
vector<int> adj[maxn];
pii res[maxn];
ll dis[maxn];
int main() {
io
int n, m;
cin >> n >> m;
for (int i = 0;i < m;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1;i <= n;i++) res[i] = {1, 1}, dis[i] = inf;
int k;
cin >> k;
for (int i = 0;i < k;i++) {
int li;
cin >> li;
for (int j = 0;j < li;j++) {
int x;
cin >> x;
res[x] = {j, li};
}
}
queue<pii> que;
dis[1] = 0;
que.push({1, 0});
while (que.size()) {
int cur = que.front().ff;
ll d = que.front().ss;
que.pop();
//debug(49, cur, d);
if (d != dis[cur]) continue;
for (int v:adj[cur]) {
int w = 1;
if ((d + 1) % res[cur].ss == res[cur].ff && d % res[v].ss == res[v].ff) {
//debug(n, v);
continue; // can't go through this edge
}
if ((d + 1) % res[v].ss == res[v].ff) w = 2;
if (d + w < dis[v]) {
//debug(14, v, d + w);
dis[v] = d + w;
que.push({v, dis[v]});
}
}
}
cout << dis[n] << "\n";
}
/*
6 6
1 2
2 3
3 4
4 5
5 2
5 6
1
4 3 2 5 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
6476 KB |
Output is correct |
2 |
Incorrect |
53 ms |
10860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
6536 KB |
Output is correct |
2 |
Incorrect |
68 ms |
10784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
6536 KB |
Output is correct |
2 |
Incorrect |
68 ms |
10784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
6536 KB |
Output is correct |
2 |
Incorrect |
68 ms |
10784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
6476 KB |
Output is correct |
2 |
Incorrect |
53 ms |
10860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
6476 KB |
Output is correct |
2 |
Incorrect |
53 ms |
10860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
6476 KB |
Output is correct |
2 |
Incorrect |
53 ms |
10860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |