結果

問題 No.848 なかよし旅行
ユーザー yuji9511yuji9511
提出日時 2019-07-05 23:48:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 2,620 bytes
コンパイル時間 1,640 ms
コンパイル使用メモリ 153,144 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2023-08-07 19:46:17
合計ジャッジ時間 4,911 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 692 ms
11,392 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,572 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 26 ms
6,048 KB
testcase_12 AC 32 ms
6,540 KB
testcase_13 AC 42 ms
7,692 KB
testcase_14 AC 15 ms
4,452 KB
testcase_15 AC 38 ms
7,528 KB
testcase_16 AC 61 ms
10,372 KB
testcase_17 AC 37 ms
8,736 KB
testcase_18 AC 24 ms
5,800 KB
testcase_19 AC 24 ms
5,500 KB
testcase_20 AC 17 ms
4,384 KB
testcase_21 AC 46 ms
8,964 KB
testcase_22 AC 40 ms
9,780 KB
testcase_23 AC 25 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 59 ms
10,272 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,508 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x,y) cout << (x) << " " << (y) << endl;
#define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " ";} cout<<endl;
ll a[100010], b[100010], c[100010];
vector<lpair> tree[2010];
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N,M,P,Q,T;
    cin >> N >> M >> P >> Q >> T;
    P--; Q--;
    rep(i,0,M){
        cin >> a[i] >> b[i] >> c[i];
        a[i]--; b[i]--;
        tree[a[i]].push_back(make_pair(b[i], c[i]));
        tree[b[i]].push_back(make_pair(a[i], c[i]));
    }
    priority_queue<lpair, vector<lpair> ,greater<lpair> > pq;
    ll dist[2010] = {};
    rep(i,1,N) dist[i] = INF;
    rep(i,0,N) pq.push(make_pair(dist[i], i));
    while(!pq.empty()){
        lpair l1 = pq.top();
        pq.pop();
        for(auto &e: tree[l1.second]){
            if(dist[e.first] > dist[l1.second] + e.second){
                dist[e.first] = dist[l1.second] + e.second;
                pq.push(make_pair(dist[e.first], e.first));
            }
        }
    }

    ll distP[2010] = {};
    rep(i,0,N) distP[i] = INF;
    distP[P] = 0;
    rep(i,0,N) pq.push(make_pair(dist[i], i));
    while(!pq.empty()){
        lpair l1 = pq.top();
        pq.pop();
        for(auto &e: tree[l1.second]){
            if(distP[e.first] > distP[l1.second] + e.second){
                distP[e.first] = distP[l1.second] + e.second;
                pq.push(make_pair(distP[e.first], e.first));
            }
        }
    }

    ll distQ[2010] = {};
    rep(i,0,N) distQ[i] = INF;
    distQ[Q] = 0;
    rep(i,0,N) pq.push(make_pair(dist[i], i));
    while(!pq.empty()){
        lpair l1 = pq.top();
        pq.pop();
        for(auto &e: tree[l1.second]){
            if(distQ[e.first] > distQ[l1.second] + e.second){
                distQ[e.first] = distQ[l1.second] + e.second;
                pq.push(make_pair(distQ[e.first], e.first));
            }
        }
    }
    ll ans = -INF;
    rep(i,0,N){
        rep(j,0,N){
            ll dd = dist[i] + max(distP[i] + distP[j], distQ[i] + distQ[j]) + dist[j];
            if(dd <= T){
                ans = max(ans, T - max(distP[i] + distP[j], distQ[i] + distQ[j]));
            }
        }
    }
    if(dist[P] + distP[Q] + dist[Q] <= T){
        print(T);
        return 0;
    }
    if(ans == -INF){
        print(-1);
    }else{
        print(ans);
    }
    
}
0