結果

問題 No.614 壊れたキャンパス
ユーザー tsutajtsutaj
提出日時 2017-12-14 13:38:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,296 ms / 2,000 ms
コード長 4,269 bytes
コンパイル時間 1,899 ms
コンパイル使用メモリ 135,524 KB
実行使用メモリ 66,148 KB
最終ジャッジ日時 2024-05-08 09:43:22
合計ジャッジ時間 11,028 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,900 KB
testcase_01 AC 5 ms
12,800 KB
testcase_02 AC 5 ms
12,896 KB
testcase_03 AC 6 ms
12,800 KB
testcase_04 AC 6 ms
12,900 KB
testcase_05 AC 6 ms
12,904 KB
testcase_06 AC 5 ms
12,928 KB
testcase_07 AC 5 ms
12,768 KB
testcase_08 AC 570 ms
40,160 KB
testcase_09 AC 1,296 ms
55,176 KB
testcase_10 AC 504 ms
43,212 KB
testcase_11 AC 626 ms
39,296 KB
testcase_12 AC 680 ms
39,168 KB
testcase_13 AC 747 ms
39,168 KB
testcase_14 AC 665 ms
40,296 KB
testcase_15 AC 427 ms
42,240 KB
testcase_16 AC 589 ms
42,848 KB
testcase_17 AC 429 ms
66,148 KB
testcase_18 AC 530 ms
42,348 KB
testcase_19 AC 443 ms
46,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 基本テンプレート

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

struct Edge {
    int to_idx, to_height;
};

struct Elem {
    int flr, cost;
    bool operator<(const Elem &x) const {
        return cost > x.cost;
    }
};

bool exist[200010];
map< int, map<int, vector<Edge> > > edges;
vector<int> in[200010], out[200010];
map<int, int> rec;

vector<int> get_array(int idx, int val) {
    int idx_l = --upper_bound(in[idx].begin(), in[idx].end(), val) - in[idx].begin();
    int idx_r =   lower_bound(in[idx].begin(), in[idx].end(), val) - in[idx].begin();

    int P = in[idx].size();
    vector<int> cand;
    if(idx_l >= 0 && idx_l < P) cand.push_back(in[idx][idx_l]);
    if(idx_r >= 0 && idx_r < P) cand.push_back(in[idx][idx_r]);
    return cand;
}

signed main() {
    int N, M, K, S, T; cin >> N >> M >> K >> S >> T;

    in[1].push_back(S);
    rep(i,0,M) {
        int a, b, c; cin >> a >> b >> c;
        exist[a] = true;
        edges[a][b].push_back(Edge{a+1, c});
        out[a].push_back(b);
        in[a+1].push_back(c);
    }

    int cnt = 0;
    repq(i,1,N) {
        sort(in[i].begin(), in[i].end());
        sort(out[i].begin(), out[i].end());
        in[i].erase(unique(in[i].begin(), in[i].end()), in[i].end());
        out[i].erase(unique(out[i].begin(), out[i].end()), out[i].end());
        cnt += exist[i];
    }

    if(cnt != N-1) {
        cout << -1 << endl;
        return 0;
    }

    if(N == 1) {
        cout << abs(S - T) << endl;
        return 0;
    }

    rec[S] = 0;
    repq(i,1,N) {
        // floor, cost
        priority_queue<Elem> tmp;
        for(auto x : rec) tmp.push(Elem{x.first, x.second});
        while(!tmp.empty()) {
            Elem cur = tmp.top(); tmp.pop();

            int idx = lower_bound(in[i].begin(), in[i].end(), cur.flr) - in[i].begin();
            int dd[] = {-1, 1};
            // printf("cur.flr = %lld, idx = %lld\n", cur.flr, idx);

            rep(k,0,2) {
                int nidx = idx + dd[k];
                if(nidx < 0 || nidx >= (int)in[i].size()) continue;
                int to = in[i][nidx];
                // printf("cur = %lld, adjacent_elem = %lld\n", cur.flr, to);
                int diff = abs(cur.flr - to);
                if(!rec.count(to) || rec[to] > rec[cur.flr] + diff) {
                    int ncost = rec[to] = rec[cur.flr] + diff;
                    tmp.push(Elem{to, ncost});
                }
            }
        }

        /*
        printf("index = %lld\n", i);
        for(auto x : rec) {
            printf("height = %lld, cost = %lld\n", x.first, x.second);
        }
        */

        map<int, int> swp;
        if(i == N) continue;
        for(auto from : out[i]) {
            int cur_cost = INF;
            vector<int> cand = get_array(i, from);
            // for(auto point : cand) printf("candidate: %lld\n", point);
            for(auto point : cand) chmin(cur_cost, rec[point] + abs(point - from));

            for(auto e : edges[i][from]) {
                int to = e.to_height;
                if(!swp.count(to) || swp[to] > cur_cost) {
                    swp[to] = cur_cost;
                }
            }
        }
        swap(rec, swp);
    }

    int ans = INF;
    for(auto x : rec) chmin(ans, x.second + abs(x.first - T));
    cout << ans << endl;
    return 0;
}
0