# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
408859 | peuch | From Hacks to Snitches (BOI21_watchmen) | C++17 | 322 ms | 34272 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;
const int MAXN = 250010;
const long long INF = 1e15;
/*
Expansão de vértice em 2:
- 2 * i -> menor tempo possível;
- 2 * i + 1 -> menor tempo possível depois que o guarda passa;
*/
int n, m, c;
long long dist[2 * MAXN];
int marc[MAXN];
int cycleLen[2 * MAXN], cycleID[2 * MAXN], cycle[2 * MAXN];
vector<int> ar[2 * MAXN];
void dijkstra();
int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++){
int a, b;
scanf("%d %d", &a, &b);
a <<= 1;
b <<= 1;
ar[a].push_back(b);
ar[a].push_back(b + 1);
ar[a + 1].push_back(b);
ar[a + 1].push_back(b + 1);
ar[b].push_back(a);
ar[b].push_back(a + 1);
ar[b + 1].push_back(a);
ar[b + 1].push_back(a + 1);
}
scanf("%d", &c);
for(int i = 1; i <= c; i++){
int l;
scanf("%d", &l);
for(int j = 0; j < l; j++){
int a, b;
scanf("%d", &a);
a <<= 1;
b = a | 1;
cycleLen[a] = cycleLen[b] = l;
cycleID[a] = cycleID[b] = j;
cycle[a] = cycle[b] = i;
}
}
dijkstra();
long long ans = min(dist[2 * n], dist[2 * n + 1]);
if(ans >= INF) printf("impossible\n");
else printf("%lld\n", ans);
}
void dijkstra(){
set<pair<long long, int> > s;
for(int i = 2; i <= 2 * n + 1; i++){
dist[i] = INF;
if((i >> 1) == 1) dist[i] = 0;
s.insert(make_pair(dist[i], i));
}
while(!s.empty()){
int cur = s.begin()->second;
marc[cur] = 1;
s.erase(s.begin());
for(int i = 0; i < ar[cur].size(); i++){
int viz = ar[cur][i];
if(marc[viz]) continue;
if((viz & 1) && cycleLen[viz] != 0){
long long tempoEsperando = (cycleID[viz] + cycleLen[viz] - (dist[cur] % cycleLen[viz])) % cycleLen[viz];
if(cycleLen[cur] != 0 && tempoEsperando >= (cycleID[cur] + cycleLen[cur] - (dist[cur] % cycleLen[cur])) % cycleLen[cur]) continue;
if(cycleLen[cur] != 0 && cycle[cur] == cycle[viz] && cycleID[cur] == (cycleID[viz] + 1) % cycleLen[cur]) continue;
long long auxD = dist[cur] + tempoEsperando + 1;
if(auxD >= dist[viz]) continue;
s.erase(make_pair(dist[viz], viz));
s.insert(make_pair(dist[viz] = auxD, viz));
}
else{
long long auxD = dist[cur] + 1;
if(cycleLen[cur] != 0 && cycle[cur] == cycle[viz] && cycleID[cur] == (cycleID[viz] + 1) % cycleLen[cur] && auxD % cycleLen[cur] == cycleID[cur]) continue;
if(cycleLen[viz] != 0 && auxD % cycleLen[viz] == cycleID[viz]) auxD++;
if(cycleLen[cur] != 0 && cycle[cur] == cycle[viz] && cycleID[cur] == (cycleID[viz] + 1) % cycleLen[cur] && auxD % cycleLen[cur] == cycleID[cur]) continue;
if(auxD >= dist[viz]) continue;
s.erase(make_pair(dist[viz], viz));
s.insert(make_pair(dist[viz] = auxD, viz));
}
}
}
}
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... |