結果

問題 No.848 なかよし旅行
ユーザー totori_nyaatotori_nyaa
提出日時 2019-08-06 15:14:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,292 bytes
コンパイル時間 2,090 ms
コンパイル使用メモリ 174,112 KB
実行使用メモリ 11,200 KB
最終ジャッジ日時 2024-04-16 10:13:41
合計ジャッジ時間 3,675 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
10,764 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 31 ms
5,764 KB
testcase_12 AC 42 ms
6,876 KB
testcase_13 AC 57 ms
8,060 KB
testcase_14 AC 17 ms
5,376 KB
testcase_15 AC 55 ms
7,812 KB
testcase_16 AC 99 ms
11,200 KB
testcase_17 AC 63 ms
8,736 KB
testcase_18 AC 29 ms
5,628 KB
testcase_19 AC 25 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 77 ms
10,212 KB
testcase_22 AC 89 ms
10,672 KB
testcase_23 AC 10 ms
5,376 KB
testcase_24 WA -
testcase_25 AC 96 ms
9,748 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

class Dijkstra {
public:
    struct edge { long long v, dist; };

    struct state {
        long long v, cost;
        bool operator>(const state s) const { return cost > s.cost; }
    };

    const long long INF = (1LL << 60);
    long long N;
    vector< vector<edge> > E;

    Dijkstra(long long n): N(n), E(n) {}

    //有効グラフの時はこっち。u→vに距離dで結ぶ
    void add_directed_edge(long long u, long long v, long long d) {
        E[u].push_back((edge) { v, d });
    }

    //無向グラフの時はこっち。uとvを双方向に距離dで結ぶ
    void add_undirected_edge(long long u, long long v, long long d) {
        E[u].push_back((edge) { v, d });
        E[v].push_back((edge) { u, d });
    }

    //Sを始点として、他の頂点への最短経路を探す
    vector<long long> shortest_path(long long S) {
        vector<long long> dp(E.size(), INF);
        priority_queue<state, vector<state>, greater<state> > q;
        q.push((state) { S, 0 });
        while(!q.empty()) {
            long long v = q.top().v, cost = q.top().cost;
            q.pop();
            if(dp[v] <= cost) continue;
            dp[v] = cost;
            for(int i=0;i < E[v].size() ; i++) {
            long long nv = E[v][i].v, ncost = cost + E[v][i].dist;
            if(dp[nv] > ncost) q.push((state) { nv, ncost });
            }
        }
        return dp;
    }
};

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

    int n,m,p,q,t;
    cin>>n>>m>>p>>q>>t;
    p--,q--;
    Dijkstra dij(n);
    for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        dij.add_undirected_edge(--a,--b,c);
    }
    vector<ll> P=dij.shortest_path(p),Q=dij.shortest_path(q),v=dij.shortest_path(0);
    ll dist = v[p] + P[q] + Q[0];
    if(dist<=t){
        cout<<t<<endl;
        return 0;
    }
    dist = max(v[p],v[q]);
    if(2*(dist)>t){
        cout<<-1<<endl;
        return 0;
    }
    ll ans=0;
    for(int i=1;i<n;i++){
        for(int j=1;j<n;j++){
            dist = v[i] + max(P[i] + P[j],Q[i] + Q[j]) + v[j];
            if(dist<=t){
                ans = max(ans, t-max(P[i] + P[j],Q[i] + Q[j]));
            } 
        }
    }
    cout<<ans<<endl;

    
}
0