結果

問題 No.614 壊れたキャンパス
ユーザー parukiparuki
提出日時 2018-01-01 22:28:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 760 ms / 2,000 ms
コード長 2,757 bytes
コンパイル時間 2,601 ms
コンパイル使用メモリ 185,908 KB
実行使用メモリ 72,004 KB
最終ジャッジ日時 2023-08-24 15:47:48
合計ジャッジ時間 9,754 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,888 KB
testcase_01 AC 6 ms
12,776 KB
testcase_02 AC 6 ms
12,684 KB
testcase_03 AC 6 ms
12,700 KB
testcase_04 AC 5 ms
12,752 KB
testcase_05 AC 5 ms
12,720 KB
testcase_06 AC 6 ms
12,724 KB
testcase_07 AC 6 ms
12,756 KB
testcase_08 AC 613 ms
70,556 KB
testcase_09 AC 760 ms
69,412 KB
testcase_10 AC 379 ms
69,000 KB
testcase_11 AC 729 ms
71,636 KB
testcase_12 AC 744 ms
71,700 KB
testcase_13 AC 718 ms
72,004 KB
testcase_14 AC 614 ms
71,908 KB
testcase_15 AC 399 ms
69,544 KB
testcase_16 AC 527 ms
70,196 KB
testcase_17 AC 169 ms
60,964 KB
testcase_18 AC 411 ms
64,680 KB
testcase_19 AC 338 ms
57,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

typedef long long Weight;
struct Edge {
    int from, to;
    Weight wei;
    Edge(int from_, int to_, Weight wei_) :from(from_), to(to_), wei(wei_) {}
};
typedef vector<Edge> Edges;
const Weight INFW = numeric_limits<Weight>::max();

struct Graph : public vector<Edges> {
    Graph() { }
    Graph(int V) : vector<Edges>(V) { }
    /*
    有向辺を追加する
    */
    void addEdge(int from, int to, Weight wei = 1) {
        (*this)[from].push_back(Edge(from, to, wei));
    }
    /*
    無向辺を追加する
    */
    void addUEdge(int u, int v, Weight wei = 1) {
        (*this)[u].push_back(Edge(u, v, wei));
        (*this)[v].push_back(Edge(v, u, wei));
    }
};

vector<Weight> dijkstra(const Graph &G, int src) {
    typedef pair<Weight, int> pwi;
    priority_queue<pwi, vector<pwi>, greater<pwi>> pq;
    pq.push(mp(0, src));
    int V = (int)G.size();
    vector<Weight> res(V, -1);
    while (pq.size()) {
        auto p = pq.top(); pq.pop();
        Weight d = p.first;
        int v = p.second;
        if (res[v] > -1)continue;
        res[v] = d;
        for (const auto &edge : G[v]) {
            int to = edge.to;
            Weight wei = edge.wei;
            pq.push(make_pair(d + wei, to));
        }
    }
    return res;
}

int N, M, K, S, T;
int L = 0;
map<int, int> id[200005];

int f(int n, int k) {
    if (!id[n].count(k)) {
        id[n][k] = L++;
    }
    assert(id[n].count(k));
    return id[n][k];
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    vector<pii> E;

    cin >> N >> M >> K >> S >> T;
    
    rep(i, M) {
        int a, b, c;
        cin >> a >> b >> c;
        E.emplace_back(f(a, b), f(a + 1, c));
    }

    f(1, S);
    f(N, T);

    Graph G(L);
    each(e, E) {
        G.addEdge(e.first, e.second, 0);
    }
    for (int i = 1; i <= N; ++i) {
        auto end = id[i].end();
        for (auto a = id[i].begin(); a != end; ++a) {
            auto b = a;
            if (++b == end)break;
            int d = b->first - a->first;
            G.addUEdge(a->second, b->second, d);
        }
    }

    auto d = dijkstra(G, f(1, S));

    ll ans = d[f(N, T)];
    if (ans == LLONG_MAX)ans = -1;
    cout << ans << endl;
}
0