이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
Solution for APIO 2015 - Skyscrapper
Tags : sqrt decomposition
*/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
a %= MOD;
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 1) return 1;
if (a == 0) return b;
return gcd(b%a, a);
}
void solve(){
int n, m; cin >> n>> m;
vi doge[n];
int dest; int start;
FOR(i,0,m){
int b, p; cin >> b >> p;
doge[b].pb(p);
if (i==0) start = b;
if (i==1) dest = b;
}
int B = floor(sqrt(n));
set<pair<int, ii>> st;
int dist[n][B+1];
FOR(i,0,n){
FOR(j,0,B+1){
dist[i][j] = MOD;
}
}
dist[start][0] = 0;
st.insert({dist[start][0], {start, 0}});
while (!st.empty()){
auto node = *st.begin();
st.erase(st.begin());
int u = node.se.fi, w = node.se.se;
if (w==0){
for (int p : doge[u]){
if (p > B){
int cur = u+p;
while (cur<n){
if (dist[cur][0] <= dist[u][w] + (cur-u)/p){
cur += p; continue;
}
if (dist[cur][0] != MOD){
st.erase({dist[cur][0], {cur, 0}});
}
dist[cur][0] = dist[u][w] + (cur-u)/p;
st.insert({dist[cur][0], {cur, 0}});
cur += p;
}
cur = u-p;
while (cur >= 0){
if (dist[cur][0] <= dist[u][w] + (u-cur)/p){
cur -= p; continue;
}
if (dist[cur][0] != MOD){
st.erase({dist[cur][0], {cur, 0}});
}
dist[cur][0] = dist[u][w] + (u-cur)/p;
st.insert({dist[cur][0], {cur, 0}});
cur -= p;
}
continue;
}
if (dist[u][p] <= dist[u][w]){
continue;
}
if (dist[u][p] != MOD){
st.erase({dist[u][p], {u, p}});
}
dist[u][p] = dist[u][w];
st.insert({dist[u][p], {u, p}});
}
continue;
}
if (dist[u][0] > dist[u][w]){
if (dist[u][0] != MOD){
st.erase({dist[u][0], {u, 0}});
}
dist[u][0] = dist[u][w];
st.insert({dist[u][0], {u, 0}});
}
if (u+w < n && dist[u+w][w] > dist[u][w] + 1){
if (dist[u+w][w] != MOD){
st.erase({dist[u+w][w], {u+w, w}});
}
dist[u+w][w] = dist[u][w] + 1;
st.insert({dist[u+w][w], {u+w, w}});
}
if (u-w >= 0 && dist[u-w][w] > dist[u][w] + 1){
if (dist[u-w][w] != MOD){
st.erase({dist[u-w][w], {u-w, w}});
}
dist[u-w][w] = dist[u][w] + 1;
st.insert({dist[u-w][w], {u-w, w}});
}
}
if (dist[dest][0] == MOD){
cout << -1;
return;
}
cout << dist[dest][0];
}
int main() {
ios::sync_with_stdio(false);
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
10 | #pragma GCC optimization ("Ofast")
|
skyscraper.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
11 | #pragma GCC optimization ("unroll-loops")
|
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:73:20: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | dist[start][0] = 0;
| ~~~~~~~~~~~~~~~^~~
skyscraper.cpp:141:21: warning: 'dest' may be used uninitialized in this function [-Wmaybe-uninitialized]
141 | if (dist[dest][0] == MOD){
| ~~~~~~~~~~~~^
# | 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... |