제출 #400703

#제출 시각아이디문제언어결과실행 시간메모리
400703BERNARB01Jakarta Skyscrapers (APIO15_skyscraper)C++17
10 / 100
4 ms6732 KiB
#include <bits/stdc++.h> using namespace std; const int N = 10; const int inf = 1e9 + 9; int n, m, target, b[N], p[N], dp[N][N][2][1 << N][8]; vector<int> g[N]; int sol(int i, int j, int dir, int vis, int dn) { if (vis & (1 << i)) { return inf; } if (i == target) { return 0; } int& ret = dp[i][j][dir][vis][dn]; if (ret != -1) { return ret; } ret = inf; int new_v = vis | (1 << i); if (dir) { if (i + p[j] < n) { ret = min(ret, 1 + sol(i + p[j], j, dir, new_v, dn)); } } else { if (i - p[j] >= 0) { ret = min(ret, 1 + sol(i - p[j], j, dir, new_v, dn)); } } for (int new_j : g[i]) { if (new_j != j && !(dn & (1 << new_j))) { ret = min(ret, sol(i, new_j, 1, 0, dn | (1 << j))); ret = min(ret, sol(i, new_j, 0, 0, dn | (1 << j))); } } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> b[i] >> p[i]; g[b[i]].push_back(i); } target = b[1]; memset(dp, -1, sizeof dp); int ans = sol(0, 0, 1, 0, 1); if (ans >= inf) { cout << -1 << '\n'; } else { cout << ans << '\n'; } return 0; }
#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...