結果

問題 No.848 なかよし旅行
ユーザー treeonetreeone
提出日時 2019-07-05 22:05:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,845 bytes
コンパイル時間 2,253 ms
コンパイル使用メモリ 207,524 KB
実行使用メモリ 10,652 KB
最終ジャッジ日時 2024-04-25 13:48:29
合計ジャッジ時間 3,624 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
10,652 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 20 ms
6,940 KB
testcase_12 AC 25 ms
6,940 KB
testcase_13 AC 31 ms
6,944 KB
testcase_14 AC 14 ms
6,940 KB
testcase_15 AC 29 ms
6,940 KB
testcase_16 AC 46 ms
8,064 KB
testcase_17 AC 32 ms
6,940 KB
testcase_18 AC 19 ms
6,940 KB
testcase_19 AC 17 ms
6,944 KB
testcase_20 AC 11 ms
6,940 KB
testcase_21 AC 38 ms
7,296 KB
testcase_22 AC 36 ms
7,424 KB
testcase_23 AC 23 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 52 ms
8,064 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

const int MAX_V = 2010;

struct edge{
   int to, cost;
   edge(int to, int cost):to(to), cost(cost){}
};

vector<edge> G[MAX_V];

struct Dijkstra{
    vector<int> d;
    Dijkstra(){}
    Dijkstra(int V){
        d.resize(V, INF);
    }
    void calc(int s){
        d[s] = 0;
        priority_queue<P, vector<P>, greater<P> > q;
        q.push(P(d[s], s));
        while(!q.empty()){
            P p = q.top(); q.pop();
            int from = p.second;
            int cost = p.first;
            if(d[from] < cost) continue;
            rep(i, 0, G[from].size()){
                int next = G[from][i].to;
                int newCost = cost + G[from][i].cost;
                if(d[next] > newCost){
                    d[next] = newCost;
                    q.push(P(newCost, next));
                }
            }
        }
    }
};

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m, p, q, t;
    cin >> n >> m >> p >> q >> t;
    p--; q--;
    rep(i, 0, m){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    Dijkstra dp(n), dq(n), ds(n);
    dp.calc(p);
    dq.calc(q);
    ds.calc(0);
    int ans = -1;
    if(ds.d[p] + dp.d[q] + ds.d[q] <= t){
        ans = t;
    }
    rep(i, 0, n){
        rep(j, 0, n){
            int tp = ds.d[i] + dp.d[i] + dp.d[j] + ds.d[j];
            int tq = ds.d[i] + dq.d[i] + dq.d[j] + ds.d[j];
            if(tp <= t && tq <= t){
                int tmp = ds.d[i] + ds.d[j] + (t - max(tp, tq));
                ans = max(ans, tmp);
            }
        }
    }
    cout << ans << endl;
}
0