This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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});
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;
for (int j=0; j<a; j++) {
cin >> b;
has_w[--b]={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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |