Submission #60846

#TimeUsernameProblemLanguageResultExecution timeMemory
60846SpeedOfMagicPinball (JOI14_pinball)C++17
51 / 100
1071 ms159112 KiB
/** MIT License Copyright (c) 2018 Vasilyev Daniil **/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template<typename T> using v = vector<T>;
#define int long long
typedef string str;
typedef vector<int> vint;
#define rep(a, l, r) for(int a = (l); a < (r); a++)
#define pb push_back
#define sz(a) ((int) a.size())
const long long inf = 4611686018427387903; //2^62 - 1
#if 0  //FileIO
const string fileName = "";
ifstream fin ((fileName == "" ? "input.txt"  : fileName + ".in" ));
ofstream fout((fileName == "" ? "output.txt" : fileName + ".out"));
#define get fin>>
#define put fout<<
#else
#define get cin>>
#define put cout<<
#endif
#define eol put endl
void read() {}     template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg)     ;read(args...) ;}
void print(){}     template<typename Arg,typename... Args> void print(Arg  arg,Args...  args){put (arg)<<" ";print(args...);}
void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg  arg,Args...  args){put (arg)<<" ";debug(args...);}
int getInt(){int a; get a; return a;}
//code goes here
const int M = 1e5 + 1;
vint g[M];
int devices[M][4];
int m = 1, n;
vint calcDistance(int d) {
    vint res(m, inf);
    res[d] = devices[d][3];
    priority_queue<pair<int, int>> dijkstra;
    dijkstra.push({-devices[d][3], d});
    while (!dijkstra.empty()) {
        int cur = dijkstra.top().second;
        int cost = -dijkstra.top().first;
        dijkstra.pop();
        if (cost != res[cur])
            continue;
        //debug(cost, cur);

        for (int i : g[cur])
            if (res[i] > cost + devices[i][3]) {
                res[i] = devices[i][3] + cost;
                dijkstra.push({-res[i], i});
            }
    }

    return res;
}

void run() {
    read(m, n);
    vint firsts, lasts;
    rep(i, 0, m) {
        read(devices[i][0], devices[i][1], devices[i][2], devices[i][3]);
        if (devices[i][0] == 1)
            firsts.pb(i);
        if (devices[i][1] == n)
            lasts.pb(i);

        rep(j, 0, i)
            if (devices[i][0] <= devices[j][2] && devices[j][2] <= devices[i][1])
                g[j].pb(i);
    }

    int posDists[m];
    rep(i, 0, m)
        posDists[i] = inf;

    for (int i : firsts) {
        vint dists = calcDistance(i);
        rep(j, 0, m)
            posDists[j] = min(posDists[j], dists[j]);
    }

    int ans = inf;
    for (int i : lasts) {
        vint dists = calcDistance(i);
        rep(j, 0, m)
            ans = min(ans, dists[j] + posDists[j] - devices[j][3]);
    }
    if (ans >= inf)
        put - 1;
    else
        put ans;
}

int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); 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...