Submission #316135

#TimeUsernameProblemLanguageResultExecution timeMemory
316135unoJakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
1 ms512 KiB
//
// Created by bg Koh on 2020/10/21.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>

using namespace std;

int N, M;
vector<vector<int>> jump;
vector<int> discovered;
vector<unordered_map<int,int>> adjList;

int dfs(int startBuild, int jmp, int time, unordered_map<int, int>& soFar, int dir )
{
    if (startBuild >= N || startBuild < 0)
        return 0;

    if (soFar.find(startBuild) != end(soFar)) {
        if (soFar[startBuild] > time)
            soFar[startBuild] = time;
    } else {
        soFar.emplace(startBuild, time);
    }
    dfs(startBuild+((dir == 1) ? jmp:-jmp), jmp, time+1, soFar, dir);
    return 1;
}

void makeGraph()
{
    for (int i=0; i<N; ++i) {
        for (int j : jump[i]) {
            unordered_map<int, int> tmp;
            dfs(i+j, j, 1, adjList[i], 1); //right
            dfs(i-j, j, 1, adjList[i], 0); //left
#if 0
            cout << "\n\n" << i << " building:" << endl;
            for (auto& k : adjList[i]) {
                cout << k.first << ":" << k.second << endl;
            }
#endif
        }
    }
}

int firstDogeLoc = 0, firstJump = 0;
int secondDogeLoc= 0, secondJump = 0;
void input()
{
    cin >> N >> M;
    jump = vector<vector<int>>(N,vector<int>());

    cin >> firstDogeLoc >> firstJump;
    jump[firstDogeLoc].emplace_back(firstJump);

    cin >> secondDogeLoc >> secondJump;
    jump[secondDogeLoc].emplace_back(secondJump);

    for (int i=2; i<M; ++i) {
        int start, j;
        cin >> start >> j;
        jump[start].emplace_back(j);
    }
    adjList = vector<unordered_map<int, int>>(N, unordered_map<int,int>());
}

vector<int> shortTime;
int dijkstra()
{
    shortTime = vector<int>(N, (int)2e9);
    priority_queue<pair<int, int>> q;

    q.emplace(0, firstDogeLoc);
    while (!q.empty()) {
        auto [dist, build] = q.top();
        dist = -dist;
        q.pop();
        if (shortTime[build] <= dist)
            continue;

        shortTime[build] = dist;
        //for (const pair<int,int>& adj : adjList[build]) {
        for (const auto& adj : adjList[build]) {
            int there= adj.first;
            int distanceToThere = adj.second + dist;
            if (shortTime[there] > distanceToThere) {
                shortTime[there] = distanceToThere;
                q.emplace(-distanceToThere, there);
            }
        }
    }
    return shortTime[secondDogeLoc] != 2e9 ? shortTime[secondDogeLoc] : -1;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    input();
    makeGraph();
    cout << dijkstra() << endl;

#if 0
    unordered_map<int,int> test;
    //dfs(2, 2, 1, test, 1);
    dfs(4, 2, 1, test, 0);

    for (auto& i : test) {
        cout << i.first << ":" << i.second << endl;
    }
#endif
    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...