結果

問題 No.614 壊れたキャンパス
ユーザー parukiparuki
提出日時 2018-01-01 22:08:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,752 bytes
コンパイル時間 2,273 ms
コンパイル使用メモリ 186,048 KB
実行使用メモリ 85,840 KB
最終ジャッジ日時 2023-08-24 15:46:35
合計ジャッジ時間 12,345 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,688 KB
testcase_01 AC 5 ms
12,876 KB
testcase_02 AC 6 ms
12,768 KB
testcase_03 AC 5 ms
12,728 KB
testcase_04 WA -
testcase_05 AC 6 ms
12,816 KB
testcase_06 WA -
testcase_07 AC 5 ms
12,700 KB
testcase_08 WA -
testcase_09 AC 1,069 ms
85,840 KB
testcase_10 AC 438 ms
75,156 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 413 ms
75,428 KB
testcase_16 WA -
testcase_17 AC 196 ms
64,064 KB
testcase_18 AC 457 ms
71,152 KB
testcase_19 AC 485 ms
71,072 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;

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

    Graph G(L);
    each(e, E) {
        G.addUEdge(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