# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
287282 | BeanZ | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 78 ms | 33792 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 ll long long
#define endl '\n'
const int N = 5e5 + 5;
ll dp[2005][2005], vis[2005], u[30005], v[30005];
vector<ll> node[2005], E[2005];
struct viet{
ll u, v, c;
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
ll n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> u[i] >> v[i];
u[i]++;
E[u[i]].push_back(i);
}
queue<viet> q;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= 2000; j++){
dp[i][j] = 1e9;
}
}
for (auto j : E[u[1]]){
q.push({u[j], v[j], 0});
dp[u[j]][v[j]] = 0;
}
vis[1] = 1;
while (q.size()){
viet x = q.front();
q.pop();
if (x.c != dp[x.u][x.v]) continue;
if ((x.u - x.v) > 0){
if (vis[x.u - x.v] == 0){
vis[x.u - x.v] = 1;
for (auto j : E[x.u - x.v]){
q.push({x.u - x.v, v[j], x.c + 1});
dp[x.u - x.v][v[j]] = x.c + 1;
}
q.push({x.u - x.v, x.v, x.c + 1});
dp[x.u - x.v][x.v] = x.c + 1;
} else {
if (dp[x.u - x.v][x.v] > (x.c + 1)){
dp[x.u - x.v][x.v] = x.c + 1;
q.push({x.u - x.v, x.v, x.c + 1});
}
}
}
if ((x.u + x.v) <= n){
if (vis[x.u + x.v] == 0){
vis[x.u + x.v] = 1;
for (auto j : E[x.u + x.v]){
q.push({x.u + x.v, v[j], x.c + 1});
dp[x.u + x.v][v[j]] = x.c + 1;
}
q.push({x.u + x.v, x.v, x.c + 1});
dp[x.u + x.v][x.v] = x.c + 1;
} else {
if (dp[x.u + x.v][x.v] > (x.c + 1)){
dp[x.u + x.v][x.v] = x.c + 1;
q.push({x.u + x.v, x.v, x.c + 1});
}
}
}
}
ll ans = 1e9;
for (int i = 1; i <= 2000; i++){
ans = min(ans, dp[u[2]][i]);
}
if (ans == 1e9) cout << -1;
else cout << ans;
}
/*
*/
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... |