Submission #953663

#TimeUsernameProblemLanguageResultExecution timeMemory
953663UnforgettableplJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1020 ms53160 KiB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define int long long

priority_queue<pair<int,pair<int,int>>> q;
vector<int> buildings[30001];
int power[30001];
bool visited[30000][201];
const int INF = 1e15;
int n;
int threshold = 200;

inline void adddoge(int x,int building,int dist){

}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int m;
    cin >> n >> m;
    threshold = sqrt(n);
    int base,tar;
    cin>>base>>power[0]>>tar>>power[1];
    buildings[base].emplace_back(power[0]);
    buildings[tar].emplace_back(power[1]);
    for(int i=2;i<m;i++){
        int b;cin>>b>>power[i];
        buildings[b].emplace_back(power[i]);
    }
    q.emplace(0, make_pair(base,0));
    while(!q.empty()){
        auto curr = q.top();q.pop();
        if(visited[curr.second.first][curr.second.second])continue;
        visited[curr.second.first][curr.second.second]=true;
        if(curr.second.first==tar){
            cout << -curr.first << '\n';
            return 0;
        }
        if(curr.second.second==0)for(int&i:buildings[curr.second.first]) {
            if(i>threshold){
                for (int j = 1; curr.second.first-j*i>= 0; ++j) {
                    int posnext = curr.second.first - j*i;
                    if (!visited[posnext][0])q.emplace(curr.first-j, make_pair(posnext, 0));
                }
                for (int j = 1; curr.second.first+j*i < n; ++j) {
                    int posnext = curr.second.first + j*i;
                    if (!visited[posnext][0])q.emplace(curr.first-j, make_pair(posnext, 0));
                }
            } else {
                if(!visited[curr.second.first][i])q.emplace(curr.first,make_pair(curr.second.first,i));
            }
        }
        else {
            if(!visited[curr.second.first][0])q.emplace(curr.first, make_pair(curr.second.first,0));
            if(curr.second.first+curr.second.second<n and !visited[curr.second.first+curr.second.second][curr.second.second])q.emplace(curr.first-1,make_pair(curr.second.first+curr.second.second,curr.second.second));
            if(curr.second.first-curr.second.second>=0 and !visited[curr.second.first-curr.second.second][curr.second.second])q.emplace(curr.first-1,make_pair(curr.second.first-curr.second.second,curr.second.second));
        }
    }
    cout << "-1\n";
}
#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...