# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
426384 | duality | From Hacks to Snitches (BOI21_watchmen) | C++11 | 6076 ms | 147324 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 mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
vi adjList[250000];
pair<int,pii> p[250000];
bool comp(int a,int b) {
return p[a] < p[b];
}
int pos[250000];
vector<LLI> dist[250000];
priority_queue<pair<LLI,int> > H;
int relax(int u,LLI d) {
if ((dist[u][d % p[u].first] == -1) || (d < dist[u][d % p[u].first])) {
dist[u][d % p[u].first] = d;
H.push(mp(-d,u));
}
return 0;
}
int done[250000],done2[250000];
int main() {
int i,j;
int N,M,K;
int u,v,l;
scanf("%d %d",&N,&M);
for (i = 0; i < M; i++) {
scanf("%d %d",&u,&v);
u--,v--;
adjList[u].pb(v);
adjList[v].pb(u);
}
scanf("%d",&K);
for (i = 0; i < K; i++) {
scanf("%d",&l);
for (j = 0; j < l; j++) scanf("%d",&v),p[v-1] = mp(l,mp(j,i));
}
for (i = 0; i < N; i++) {
if (p[i].first == 0) p[i].first = 1;
dist[i].resize(p[i].first);
fill(dist[i].begin(),dist[i].end(),-1);
}
for (i = 0; i < N; i++) {
sort(adjList[i].begin(),adjList[i].end(),comp);
while ((pos[i] < adjList[i].size()) && (p[adjList[i][pos[i]]].first == 1)) pos[i]++;
}
dist[0][0] = 0,H.push(mp(0,0));
while (!H.empty()) {
int u = H.top().second;
LLI d = -H.top().first;
H.pop();
if (d > dist[u][d % p[u].first]) continue;
else if (u == N-1) {
printf("%lld\n",d);
return 0;
}
if ((p[u].first == 1) || (((d+1) % p[u].first) != p[u].second.first)) relax(u,d+1);
for (i = done[u] ? pos[u]:0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (p[v].first == 1) relax(v,d+1);
else if (p[u].first == 1) {
if (done2[v]) continue;
else {
for (j = 0; j < p[v].first; j++) {
LLI d2 = d+j+1;
if ((d2 % p[v].first) != p[v].second.first) relax(v,d2);
}
done2[v] = 1;
}
}
else if (p[u].second.second == p[v].second.second) {
if (((d+1) % p[v].first) == p[v].second.first) continue;
if (((d % p[v].first) == p[v].second.first) && (((d+1) % p[u].first) == p[u].second.first)) continue;
relax(v,d+1);
}
else {
for (j = 0; j < p[v].first; j++) {
LLI d2 = d+(LLI) j*p[u].first+1;
if ((d2 % p[v].first) != p[v].second.first) relax(v,d2);
}
}
}
done[u] = 1;
}
printf("impossible\n");
return 0;
}
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... |