結果

問題 No.614 壊れたキャンパス
ユーザー tsutajtsutaj
提出日時 2017-12-14 00:43:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,234 bytes
コンパイル時間 1,438 ms
コンパイル使用メモリ 122,564 KB
実行使用メモリ 29,060 KB
最終ジャッジ日時 2024-05-08 08:09:38
合計ジャッジ時間 8,362 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,568 KB
testcase_01 AC 6 ms
8,192 KB
testcase_02 AC 6 ms
8,192 KB
testcase_03 AC 6 ms
8,192 KB
testcase_04 AC 6 ms
8,192 KB
testcase_05 AC 6 ms
8,064 KB
testcase_06 AC 6 ms
8,192 KB
testcase_07 AC 6 ms
8,320 KB
testcase_08 TLE -
testcase_09 AC 509 ms
29,060 KB
testcase_10 AC 276 ms
13,312 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

vector<pii> edges[200010];
map< int, map<int, int> > rec;

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

signed main() {
    int N, M, K, S, T; cin >> N >> M >> K >> S >> T;
    rep(i,0,M) {
        int a, b, c; cin >> a >> b >> c;
        edges[a].push_back(make_pair(b, c));
    }

    priority_queue<Elem> que;
    que.push(Elem{1, S, 0});
    rec[1][S] = 0;
    while(!que.empty()) {
        Elem cur = que.top(); que.pop();
        int idx = cur.idx, flr = cur.flr, cost = cur.cost;

        for(auto e : edges[idx]) {
            int cur_flr = e.first, nxt_flr = e.second;
            int dist = abs(flr - cur_flr);
            if(!rec[idx+1].count(nxt_flr) || rec[idx+1][nxt_flr] > cost + dist) {
                int ncost = rec[idx+1][nxt_flr] = cost + dist;
                que.push(Elem{idx+1, nxt_flr, ncost});
            }
        }
    }

    int ans = INF;
    for(auto x : rec[N]) {
        int flr = x.first, cost = x.second;
        // printf("flr = %lld, cost = %lld\n", flr, cost);
        chmin(ans, cost + abs(flr - T));
    }
    if(ans == INF) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}
0