# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
406219 | tqbfjotld | From Hacks to Snitches (BOI21_watchmen) | C++14 | 2518 ms | 524292 KiB |
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 int long long
int special[1000005];
vector<pair<int,int> > orig;
vector<int> routes[2755];
int cnt = 1;
vector<int> nodes[1000005];
vector<int> new_adjl[1000005];
int dist[1000005];
int dist2[1000005];
int tt[1000005];
pair<int,int> rev[1000005];
main(){
int n,m;
scanf("%lld%lld",&n,&m);
for (int x = 0; x<m; x++){
int a,b;
scanf("%lld%lld",&a,&b);
orig.push_back({a,b});
}
int K;
scanf("%lld",&K);
for (int x = 0; x<K; x++){
int l;
scanf("%lld",&l);
vector<int> stuff;
for (int y = 0; y<l; y++){
int a;
scanf("%lld",&a);
stuff.push_back(a);
special[a] = l;
tt[a] = y;
}
}
for (int x = 1; x<=n; x++){
for (int y = 0; y<special[x]; y++){
rev[cnt] = {y,special[x]};
dist2[cnt] = 999999999;
nodes[x].push_back(cnt++);
}
if (special[x]==0){
rev[cnt] = {0,0};
dist2[cnt] = 999999999;
nodes[x].push_back(cnt++);
}
}
for (auto x : orig){
if (special[x.first]&&special[x.second]){
int l = special[x.first];
for (int t = 0; t<l; t++){
if (tt[x.first]!=t && tt[x.second]!=(t+1)%l &&
(!(tt[x.first]==(t+1)%l && tt[x.second]==t)))
new_adjl[nodes[x.first][t]].push_back(nodes[x.second][(t+1)%l]);
if (tt[x.second]!=t && tt[x.first]!=(t+1)%l &&
(!(tt[x.second]==(t+1)%l && tt[x.first]==t)))
new_adjl[nodes[x.second][t]].push_back(nodes[x.first][(t+1)%l]);
}
}
else if (special[x.first]){
int l = special[x.first];
for (int t = 0; t<l; t++){
if (tt[x.first]!=t) new_adjl[nodes[x.first][t]].push_back(nodes[x.second][0]);
if (tt[x.first]!=(t+1)%l) new_adjl[nodes[x.second][0]].push_back(nodes[x.first][(t+1)%l]);
}
}
else if (special[x.second]){
int l = special[x.second];
for (int t = 0; t<l; t++){
if (tt[x.second]!=(t+1)%l) new_adjl[nodes[x.first][0]].push_back(nodes[x.second][(t+1)%l]);
if (tt[x.second]!=t) new_adjl[nodes[x.second][t]].push_back(nodes[x.first][0]);
}
}
else{
new_adjl[nodes[x.first][0]].push_back(nodes[x.second][0]);
new_adjl[nodes[x.second][0]].push_back(nodes[x.first][0]);
}
}
for (int x = 1; x<=n; x++){
if (special[x]){
for (int t = 0; t<special[x]; t++){
if (t==tt[x]) continue;
if ((t+1)%special[x]==tt[x]) continue;
new_adjl[nodes[x][t]].push_back(nodes[x][(t+1)%special[x]]);
}
}
}
memset(dist,-1,sizeof(dist));
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
q.push({0,nodes[1][0]});
while (!q.empty()){
int node = q.top().second;
int d = q.top().first;
q.pop();
if (dist[node]!=-1) continue;
if (rev[node].second){
if (d%rev[node].second!=rev[node].first){
int del = (rev[node].first-d)%rev[node].second;
if (del<0) del += rev[node].second;
d += del;
q.push({d,node});
continue;
}
}
//printf("node %lld, %lld\n",node,d);
dist[node] = d;
for (auto x : new_adjl[node]){
if (d+1<dist2[x]){
q.push({d+1,x});
dist2[x] = d+1;
}
}
}
int ans = dist[nodes[n][0]];
printf(ans==-1?"impossible":"%lld",ans);
}
Compilation message (stderr)
# | 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... |