# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
408859 | peuch | From Hacks to Snitches (BOI21_watchmen) | C++17 | 322 ms | 34272 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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));
}
}
}
}
컴파일 시 표준 에러 (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... |