結果

問題 No.848 なかよし旅行
ユーザー onakaT_TitaionakaT_Titai
提出日時 2019-07-23 20:27:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 3,556 bytes
コンパイル時間 1,201 ms
コンパイル使用メモリ 122,380 KB
実行使用メモリ 9,724 KB
最終ジャッジ日時 2023-08-07 19:50:57
合計ジャッジ時間 3,605 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
9,724 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 33 ms
4,892 KB
testcase_12 AC 44 ms
5,072 KB
testcase_13 AC 58 ms
5,928 KB
testcase_14 AC 21 ms
4,380 KB
testcase_15 AC 55 ms
5,684 KB
testcase_16 AC 94 ms
6,964 KB
testcase_17 AC 65 ms
6,220 KB
testcase_18 AC 34 ms
4,820 KB
testcase_19 AC 29 ms
4,380 KB
testcase_20 AC 14 ms
4,380 KB
testcase_21 AC 80 ms
6,452 KB
testcase_22 AC 83 ms
6,292 KB
testcase_23 AC 24 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 100 ms
7,016 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
#include <cassert>
#include <random>
#include <time.h>
//#include <boost/multiprecision/cpp_int.hpp>



using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const double EPS = 1e-10;
long long const MOD = 1000000007;

long long mod_pow(long long x, long long n) {
    long long res = 1;
    for (int i = 0;i < 60; i++) {
        if (n >> i & 1) res = res * x % MOD;
        x = x * x % MOD;
    }
    return res;
}

template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

template<typename T>
T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

void fastInput() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

struct Edge{
    int from, to, cost;
    bool operator <(const Edge &e) const {
        return cost < e.cost;
    }
    bool operator >(const Edge &e) const {
        return cost > e.cost;
    }
    bool operator ==(const Edge &e) const {
        return cost == e.cost;
    }
    bool operator !=(const Edge &e) const {
        return cost != e.cost;
    }
};

template <typename T>
vector<T> dijkstra(vector<vector<Edge>> &G, int s) {
    int V = G.size();
    vector<T> d(V); fill(d.begin(), d.end(), numeric_limits<T>::max());
    priority_queue<pair<T, int> , vector<pair<T, int>>, greater<pair<T, int>>> que;
    d[s] = 0;
    que.push(make_pair(0,s));

    while (!que.empty()) {
        pair<T, int> p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i=0; i<G[v].size(); i++) {
            Edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(make_pair(d[e.to], e.to));
            }
        }
    }
    return d;
}

vector<vector<Edge>> G;
vector<Int> DistFromStart;
vector<Int> DistFromP;
vector<Int> DistFromQ;
Int T;


int main(void) {
    Int N, M, P, Q;
    cin >> N >> M >> P >> Q >> T;
    P--, Q--;
    G = vector<vector<Edge>>(N);
    for (int i = 0; i < M; i++) {
        int a, b, c; cin >> a >> b >> c;
        a--, b--, c;
        G[a].push_back({a, b, c});
        G[b].push_back({b, a, c});
    }
    DistFromStart = dijkstra<Int>(G, 0);
    DistFromP = dijkstra<Int>(G, P);
    DistFromQ = dijkstra<Int>(G, Q);


    Int ans = -1;
    for (int i = 0; i < G.size(); i++) {
        for (int j = 0; j < G.size(); j++) {
            Int sum = 0;
            sum += DistFromStart[i];
            sum += max(DistFromP[i]+DistFromP[j], DistFromQ[i]+DistFromQ[j]);
            sum += DistFromStart[j];
            if (sum <= T) {
                ans = max(DistFromStart[i]+DistFromStart[j]+T-sum, ans);
            }
        }
    }
    if (DistFromStart[P] + DistFromP[Q] + DistFromQ[0] <= T) {
        Int tmp = DistFromStart[P] + DistFromP[Q] + DistFromP[0];
        ans = max(DistFromStart[P] + DistFromP[Q] + DistFromP[0] + T - tmp, ans);
    }
    if (DistFromStart[Q] + DistFromQ[P] + DistFromP[0] <= T) {
        Int tmp = DistFromStart[Q] + DistFromQ[P] + DistFromP[0];
        ans = max(DistFromStart[Q] + DistFromQ[P] + DistFromP[0] + T - tmp, ans);
    }
    cout << ans << endl;
    return 0;
}
0