제출 #239115

#제출 시각아이디문제언어결과실행 시간메모리
239115jhnah917Jakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
850 ms3700 KiB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> p;

int n, m, s, t;
vector<int> g[30303];
int dst[30303];

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for(int i=0; i<m; i++){
        int a, b; cin >> a >> b;
        g[a].push_back(b);
        if(i == 0) s = a;
        else if(i == 1) t = a;
    }
    memset(dst, 0x3f, sizeof dst);
    priority_queue<p> pq;
    pq.emplace(0, s); dst[s] = 0;
    while(pq.size()){
        int now = pq.top().second;
        int cst = -pq.top().first;
        pq.pop();
        if(dst[now] < cst) continue;
        for(auto i : g[now]){
            for(int j=0; now+i*j<n; j++) if(dst[now+i*j] > cst + j){
                dst[now+i*j] = cst + j;
                pq.emplace(-(cst+j), now+i*j);
            }
            for(int j=1; now-i*j>=0; j++) if(dst[now-i*j] > cst + j){
                dst[now-i*j] = cst + j;
                pq.emplace(-(cst+j), now-i*j);
            }
        }
    }
    if(dst[t] == 0x3f3f3f3f) cout << -1;
    else cout << dst[t];
}
#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...