Submission #287258

#TimeUsernameProblemLanguageResultExecution timeMemory
287258BeanZJakarta Skyscrapers (APIO15_skyscraper)C++14
0 / 100
1 ms512 KiB
#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[2][i]);
        }
        if (ans == 1e9) cout << -1;
        else cout << ans;
}
/*
*/

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   17 |                 freopen("VietCT.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:18:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   18 |                 freopen("VietCT.OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...