제출 #786101

#제출 시각아이디문제언어결과실행 시간메모리
786101devariaotaJakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
8 ms16768 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define fi first #define se second queue<pair<int, int>> q; vector<int> adj[30005]; int dist[2005][2005]; bool vis[2005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); } for (int i = 0; i <= 2000; i++) { for (int j = 0; j <= 2000; j++) dist[i][j] = -1; } for (auto x : adj[0]) { q.push({0, x}); dist[0][x] = 0; } vis[0] = 1; while (!q.empty()) { int x = q.front().fi, d = q.front().se; q.pop(); if (x == 1) { cout << dist[x][d] << "\n"; return 0; } if (x - d >= 0 and !vis[x - d]) { vis[x - d] = 1; for (auto u : adj[x - d]) { q.push({x - d, u}); dist[x - d][u] = dist[x][d] + 1; } } if (x - d >= 0 and dist[x - d][d] == -1) { q.push({x - d, d}); dist[x - d][d] = dist[x][d] + 1; } if (x + d < n and !vis[x + d]) { vis[x + d] = 1; for (auto u : adj[x + d]) { q.push({x + d, u}); dist[x + d][u] = dist[x][d] + 1; } } if (x + d < n and dist[x + d][d] == -1) { q.push({x + d, d}); dist[x + d][d] = dist[x][d] + 1; } } cout << -1 << "\n"; }
#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...