結果

問題 No.614 壊れたキャンパス
ユーザー tsutajtsutaj
提出日時 2017-12-14 13:12:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,918 bytes
コンパイル時間 1,624 ms
コンパイル使用メモリ 135,824 KB
実行使用メモリ 66,148 KB
最終ジャッジ日時 2024-05-08 09:42:07
合計ジャッジ時間 9,675 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
12,900 KB
testcase_01 AC 4 ms
12,900 KB
testcase_02 AC 5 ms
12,900 KB
testcase_03 AC 6 ms
12,900 KB
testcase_04 AC 5 ms
13,028 KB
testcase_05 AC 5 ms
12,900 KB
testcase_06 AC 5 ms
13,024 KB
testcase_07 AC 5 ms
12,900 KB
testcase_08 WA -
testcase_09 AC 950 ms
55,436 KB
testcase_10 AC 486 ms
43,364 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 415 ms
42,340 KB
testcase_16 WA -
testcase_17 AC 449 ms
66,148 KB
testcase_18 AC 535 ms
42,356 KB
testcase_19 AC 439 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();

            vector<int> cand = get_array(i, cur.flr);
            for(auto to : cand) {
                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