이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define LL_MAX LONG_LONG_MAX
#define LL_MIN LONG_LONG_MIN
#define speed ios_base::sync_with_stdio(false); cin.tie(0)
#define stMx(a,b) a = max(a,b)
#define stMn(a,b) a = min(a,b)
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef set<int> si;
typedef vector<ii> vii;
typedef set<ii> sii;
typedef pair<int,ii> iii;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define HASTC 0
#define BSIZE 200
vector<pair<ii,int>> adj[30069][BSIZE+5];
int dist[30069][BSIZE+5];
void dijkstra(ii s) {
priority_queue<iii,vector<iii>,greater<iii>> pq;
REP(i,0,30069) REP(j,0,BSIZE+5) dist[i][j]=1210201042069;
dist[s.fi][s.se]=0; pq.push(iii(0,s));
while(!pq.empty()) {
iii c=pq.top(); pq.pop();
if(c.fi!=dist[c.se.fi][c.se.se]) continue;
for(auto i: adj[c.se.fi][c.se.se]) {
if(dist[i.fi.fi][i.fi.se]>c.fi+i.se) {
dist[i.fi.fi][i.fi.se]=c.fi+i.se;
pq.push(iii(dist[i.fi.fi][i.fi.se], i.fi));
}
}
}
}
void solvetc(int idx) {
int N, M, B, P; cin >> N >> M;
ii s, d;
REP(i,0,N) REP(j,1,BSIZE+1) {
// connect pos pow to (pos+pow,pow),(pos-pow,pow),(pos+pow),(pos-pow)
if(i-j>=0) {
adj[i][j].pb(mp(mp(i-j,j),1));
adj[i][j].pb(mp(mp(i-j,0),1));
}
if(i+j<N) {
adj[i][j].pb(mp(mp(i+j,j),1));
adj[i][j].pb(mp(mp(i+j,0),1));
}
}
REP(i,0,M) {
cin>>B>>P;
if(i==0) s=mp(B,0);
else if(i==1) d=mp(B,0);
if(P<=BSIZE) {
adj[B][0].pb(mp(mp(B,P),0));
adj[B][P].pb(mp(mp(B,0),0));
} else REP(i,-(B/P),(N-B-1)/P+1) adj[B][0].pb(mp(mp(P*i+B,0),abs(i)));
}
dijkstra(s);
cout<<(dist[d.fi][d.se]!=1210201042069?dist[d.fi][d.se]:-1);
}
int32_t main() {
int tc;
if(HASTC) cin>>tc;
else tc=1;
for(int i=0;i<tc;i++) solvetc(i);
}
# | 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... |