# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45848 | antimirage | Jakarta Skyscrapers (APIO15_skyscraper) | C++17 | 2 ms | 620 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>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define OK puts("OK");
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 2005;
int n, m, u[N][N], pos[N], pw[N], ans = 1e9 + 7;
vector <int> vec[N];
queue <pair <int, int> > q;
main()
{
cin >> n >> m;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &pos[i], &pw[i]);
vec[pos[i]].pb(pw[i]);
}
for (auto to : vec[ pos[0] ])
{
q.push( mk(pos[0], to) );
u[ pos[0] ][to] = 1;
}
vec[ pos[0] ].clear();
while (!q.empty() )
{
int v = q.front().fr, cur_pow = q.front().sc;
q.pop();
for (auto it : vec[v])
{
if (!u[v][it])
{
u[v][it] = u[v][cur_pow];
q.push( mk(v, it) );
}
}
vec[v].clear();
if ( v - cur_pow >= 0 && !u[v - cur_pow][cur_pow] )
{
u[v - cur_pow][cur_pow] = u[v][cur_pow] + 1;
q.push( mk(v - cur_pow, cur_pow) );
}
if ( v + cur_pow < n && !u[v + cur_pow][cur_pow] )
{
u[v + cur_pow][cur_pow] = u[v][cur_pow] + 1;
q.push( mk(v + cur_pow, cur_pow) );
}
}
for (int i = 0; i < m; i++)
{
if (u[ pos[1] ][i])
ans = min(ans, u[ pos[1] ][i] - 1);
}
if (ans == 1e9 + 7) ans = -1;
cout << ans << endl;
}
/**
5 3
0 2
1 1
4 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... |