#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define all(x) (x).begin(),(x).end()
const int maxn=250000+10;
vector<vi> graph(maxn);
vector<pi> has_w(maxn,{-1,-1});
vector<vi> watch(1000);
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
int a,b;
for (int i=0; i<m; i++) {
cin >> a >> b;
graph[--a].pb(--b);
graph[b].pb(a);
}
int k;
cin >> k;
vi empt;
for (int i=0; i<k; i++) {
cin >> a;
watch[i].resize(a);
for (int j=0; j<a; j++) {
cin >> watch[i][j];
has_w[--watch[i][j]]={a,j};
}
}
priority_queue<pi,vector<pi>,greater<pi>> q;
q.emplace(0,0);
vi mintime(n,1e9);
mintime[0]=0;
while (q.size()) {
auto [time,cur]=q.top();
q.pop();
if (cur==n-1) {
cout << time << '\n';
return 0;
}
if (has_w[cur].fi==-1 && mintime[cur]!=time) {
continue;
}
if (has_w[cur].fi!=-1 && mintime[cur]+has_w[cur].fi<time) {
continue;
}
for (int to:graph[cur]) {
if (has_w[to].fi==-1) {
if (mintime[to]>1+time) {
mintime[to]=1+time;
q.emplace(1+time,to);
}
}
else if (has_w[cur].fi!=-1) {
if (time%has_w[to].fi==has_w[to].se && (time+1)%has_w[cur].fi==has_w[cur].se) {
continue;
}
if ((time+1)%has_w[to].fi==has_w[to].se) {
continue;
}
if (mintime[to]+has_w[to].fi>1+time) {
mintime[to]=min(1+time,mintime[to]);
q.emplace(1+time,to);
}
}
else {
if ((time+1)%has_w[to].fi!=has_w[to].se) {
if (mintime[to]+has_w[to].fi>1+time) {
mintime[to]=min(1+time,mintime[to]);
q.emplace(1+time,to);
}
}
int t=((1ll*time-has_w[to].se+has_w[to].fi)/has_w[to].fi)*has_w[to].fi+1+has_w[to].se;
if (mintime[to]+has_w[to].fi>t) {
mintime[to]=min(t,mintime[to]);
q.emplace(t,to);
}
}
}
}
cout << "impossible\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10960 KB |
Output is correct |
2 |
Runtime error |
4694 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10968 KB |
Output is correct |
2 |
Runtime error |
4541 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10968 KB |
Output is correct |
2 |
Runtime error |
4541 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10968 KB |
Output is correct |
2 |
Runtime error |
4541 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10960 KB |
Output is correct |
2 |
Runtime error |
4694 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10960 KB |
Output is correct |
2 |
Runtime error |
4694 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10960 KB |
Output is correct |
2 |
Runtime error |
4694 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |