結果

問題 No.848 なかよし旅行
ユーザー finefine
提出日時 2019-07-05 22:23:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 2,472 bytes
コンパイル時間 1,791 ms
コンパイル使用メモリ 176,620 KB
実行使用メモリ 11,224 KB
最終ジャッジ日時 2023-08-07 19:41:39
合計ジャッジ時間 3,759 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
10,536 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 26 ms
5,736 KB
testcase_12 AC 35 ms
6,820 KB
testcase_13 AC 48 ms
7,840 KB
testcase_14 AC 13 ms
4,376 KB
testcase_15 AC 45 ms
7,696 KB
testcase_16 AC 83 ms
11,224 KB
testcase_17 AC 53 ms
8,708 KB
testcase_18 AC 24 ms
5,324 KB
testcase_19 AC 20 ms
4,900 KB
testcase_20 AC 8 ms
4,376 KB
testcase_21 AC 67 ms
9,976 KB
testcase_22 AC 76 ms
10,716 KB
testcase_23 AC 19 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 83 ms
9,624 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct State {
    int at;
    ll cost;
    //int prev;
    State(int at, ll cost/*, int prev*/) : at(at), cost(cost)/*, prev(prev)*/ {}
    bool operator>(const State& s) const {
        //if (cost != s.cost) return cost > s.cost;
        //if (prev != s.prev) return prev > s.prev; //最短経路を辞書順最小にする(省略可)
        //return at > s.at;
        return cost > s.cost;
    }
};

struct Edge {
    int to;
    ll cost;
    Edge(int to, ll cost) : to(to), cost(cost) {}
};

using Graph = vector<vector<Edge> >; //隣接リスト

const ll INF = 1e17;
//const int NONE = -1;

//sは始点、mincは最短経路のコスト、Prevは最短経路をたどる際の前の頂点
void dijkstra(int s, const Graph& graph, vector<ll>& minc/*, vector<int>& Prev*/){
    minc.assign(graph.size(), INF);
    //Prev.assign(graph.size(), INF);
    priority_queue<State, vector<State>, greater<State> > pq;
    pq.emplace(s, 0/*, NONE*/);
    while(!pq.empty()) {
        State cur = pq.top();
        pq.pop();
        if (minc[cur.at] <= cur.cost) continue;
        minc[cur.at] = cur.cost;
        //Prev[cur.at] = cur.prev;
        for(const Edge& e : graph[cur.at]) {
            ll cost = cur.cost + e.cost;
            if (minc[e.to] <= cost) continue;
            pq.emplace(e.to, cost/*, cur.at*/);
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, p, q;
    ll t;
    cin >> n >> m >> p >> q >> t;
    p--; q--;

    Graph g(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--; b--;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }

    vector<ll> minc1;
    dijkstra(0, g, minc1);

    vector<ll> mincp;
    dijkstra(p, g, mincp);

    vector<ll> mincq;
    dijkstra(q, g, mincq);

    ll ans = -1;
    {
        ll cost = minc1[p] + mincp[q] + mincq[0];
        if (cost <= t) ans = max(ans, t);
    }

    {
        ll cost = minc1[q] + mincq[p] + mincp[0];
        if (cost <= t) ans = max(ans, t);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ll cost = minc1[i] + max(mincp[i] + mincp[j], mincq[i] + mincq[j]) + minc1[j];
            if (cost > t) continue;
            ll score = minc1[i] + minc1[j] + t - cost;
            ans = max(ans, score);
        }
    }
    cout << ans << "\n";
    return 0;
}
0