#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 250110;
const int inf = (int)1e9;
vector<int> T[N];
int id[N];
int mod[N];
int ash[N];
vector<int> ord[N];
vector<int> dp[N];
vector<bool> vis[N];
struct item{
int dist;
int node;
int inv;
bool operator<(const item &q) const {
return dist > q.dist;
}
};
int main(){
fastIO;
int n, m;
cin >> n >> m;
int ui, vi;
for(int i = 1; i <= m; i ++ ){
cin >> ui >> vi;
T[ui].push_back(vi);
T[vi].push_back(ui);
}
int k;
cin >> k;
int sz;
for(int iq = 1; iq <= k ; iq ++ ){
cin >> sz;
for(int j = 0; j < sz; j ++ ){
cin >> ui;
ash[ui] = j;
mod[ui] = sz;
id[ui] = iq;
ord[iq].push_back(ui);
}
}
for(int i = 1; i <= n; i ++ ){
if(id[i] == 0){
vis[i].resize(1, false);
dp[i].resize(1, inf);
}
else{
vis[i].resize(mod[i], false);
dp[i].resize(mod[i], inf);
}
}
priority_queue<item> pq;
dp[1][0] = 0;
pq.push({0, 1, 0});
int node;
int iinv;
int dis;
int ban0, ban1;
int nex;
while(!pq.empty()){
dis = pq.top().dist;
node = pq.top().node;
iinv = pq.top().inv;
pq.pop();
if(vis[node][iinv]) continue;
vis[node][iinv] = true;
if(id[node] == 0){
for(auto x : T[node]){
if(id[x] == 0){
if(dp[x][0] > dis + 1){
dp[x][0] = dis + 1;
pq.push({dp[x][0], x, 0});
}
}
else{
for(int wait = 1; wait <= mod[x]; wait ++ ){
if(ord[id[x]][(wait + dis) % mod[x]] != x && dp[x][(wait + dis) % mod[x]] > dis + wait){
dp[x][(wait + dis) % mod[x]] = dis + wait;
pq.push({dp[x][(wait + dis) % mod[x]], x, (wait + dis) % mod[x]});
}
}
}
}
}
else{
for(auto x : T[node]){
if(id[x] == 0){
if(dp[x][0] > dis + 1){
dp[x][0] = dis + 1;
pq.push({dp[x][0], x, 0});
}
}
}
ban0 = ord[id[node]][iinv];
ban1 = ord[id[node]][(iinv - 1 + mod[node]) % mod[node]];
nex = ord[id[node]][(ash[node] + 1) % mod[node]];
if(dp[nex][(iinv + 1) % mod[nex]] > dis + 1){
dp[nex][(iinv + 1) % mod[nex]] = dis + 1;
pq.push({dis + 1, nex, (iinv + 1) % mod[nex]});
}
nex = ord[id[node]][(ash[node] - 1 + mod[node]) % mod[node]];
if(nex != ban0 && nex != ban1 && dp[nex][(iinv + 1) % mod[nex]] > dis + 1){
dp[nex][(iinv + 1) % mod[nex]] = dis + 1;
pq.push({dis + 1, nex, (iinv + 1) % mod[nex]});
}
}
}
if(dp[n][0] < inf)
cout << dp[n][0] << "\n";
else
cout << "impossible\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
29584 KB |
Output is correct |
2 |
Correct |
116 ms |
39128 KB |
Output is correct |
3 |
Incorrect |
107 ms |
38516 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
29576 KB |
Output is correct |
2 |
Correct |
112 ms |
39140 KB |
Output is correct |
3 |
Incorrect |
112 ms |
38480 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
29576 KB |
Output is correct |
2 |
Correct |
112 ms |
39140 KB |
Output is correct |
3 |
Incorrect |
112 ms |
38480 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
29576 KB |
Output is correct |
2 |
Correct |
112 ms |
39140 KB |
Output is correct |
3 |
Incorrect |
112 ms |
38480 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
29584 KB |
Output is correct |
2 |
Correct |
116 ms |
39128 KB |
Output is correct |
3 |
Incorrect |
107 ms |
38516 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
29584 KB |
Output is correct |
2 |
Correct |
116 ms |
39128 KB |
Output is correct |
3 |
Incorrect |
107 ms |
38516 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
29584 KB |
Output is correct |
2 |
Correct |
116 ms |
39128 KB |
Output is correct |
3 |
Incorrect |
107 ms |
38516 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |