제출 #927154

#제출 시각아이디문제언어결과실행 시간메모리
927154Amirreza_FakhriJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
146 ms4952 KiB
// In the name of the God #include <bits/stdc++.h> #define ll long long // #define int long long #define pb push_back #define F first #define S second #define mp make_pair #define pii pair <int, int> #define smin(x, y) (x) = min((x), (y)) #define smax(x, y) (x) = max((x), (y)) #define all(x) (x).begin(), (x).end() using namespace std; const int inf = 1e9+7; const int mod = 998244353; const int maxn = 3e4+5; int n, m, s, t, dist[maxn]; set <int> doge[maxn]; set <pii> st; void dij(int v) { fill(dist, dist+n, inf); dist[v] = 0; st.insert(mp(0, v)); while (st.size()) { int d = st.begin()->F, u = st.begin()->S; st.erase(st.begin()); // if (u == t) return cout << d << '\n', void(); for (int p : doge[u]) { for (int j = 1; u+j*p < n; j++) { if (dist[u+j*p] > d+j) { st.erase(mp(dist[u+j*p], u+j*p)); dist[u+j*p] = d+j; st.insert(mp(d+j, u+j*p)); } if (doge[u+j*p].find(p) != doge[u+j*p].end()) break; } for (int j = 1; u-j*p >= 0; j++) { if (dist[u-j*p] > d+j) { st.erase(mp(dist[u-j*p], u-j*p)); dist[u-j*p] = d+j; st.insert(mp(d+j, u-j*p)); } if (doge[u-j*p].find(p) != doge[u-j*p].end()) break; } } } // cout << -1 << '\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; doge[a].insert(b); if (i == 0) s = a; if (i == 1) t = a; } dij(s); cout << (dist[t] == inf ? -1 : dist[t]) << '\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...