# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
116481 | roseanne_pcy | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 12 ms | 3072 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>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
const int maxn = 3e4+5;
const int half = 173;
int n, m;
int dist[maxn];
set<int> unvis[180][180];
vector<int> png[maxn];
bool good(int x)
{
return 0<= x && x< n;
}
void bfs(int s)
{
for(int i = 0; i< n; i++) dist[i] = 1e9;
queue<int> q;
q.push(s); dist[s] = 0;
while(!q.empty())
{
int u = q.front(); q.pop();
// printf("%d\n", u);
for(int pw : png[u])
{
if(pw> half)
{
int cur = u;
int w = 1;
while(cur< n)
{
int nxt = cur+pw;
if(good(nxt) && dist[nxt]> w+dist[u])
{
dist[nxt] = w+dist[u];
for(int i = 1; i<= half; i++) unvis[i][nxt%i].erase(nxt);
q.push(nxt);
}
w++;
cur = nxt;
}
cur = u;
w = 1;
while(cur>= 0)
{
int nxt = cur-pw;
if(good(nxt) && dist[nxt]> w+dist[u])
{
dist[nxt] = w+dist[u];
for(int i = 1; i<= half; i++) unvis[i][nxt%i].erase(nxt);
q.push(nxt);
}
cur = nxt;
}
}
else
{
for(int nxt : unvis[pw][u%pw])
{
// if(u == 4) printf("go %d\n", nxt);
if(dist[nxt]> abs(u-nxt)/pw+dist[u])
{
dist[nxt] = abs(u-nxt)/pw+dist[u];
for(int i = 1; i<= half; i++) unvis[i][nxt%i].erase(nxt);
q.push(nxt);
}
}
unvis[pw][u%pw].clear();
}
}
}
}
int hom[maxn], mana[maxn];
int main()
{
scanf("%d %d", &n, &m);
for(int i = 0; i< m; i++)
{
scanf("%d %d", &hom[i], &mana[i]);
png[hom[i]].pb(mana[i]);
}
for(int md = 1; md<= half; md++)
{
for(int i = 0; i< n; i++)
{
unvis[md][i%md].insert(i);
}
}
// printf("okay\n");
bfs(hom[0]);
if(dist[hom[1]] == 1e9) printf("-1\n");
else printf("%d\n", dist[hom[1]]);
}
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... |