結果

問題 No.848 なかよし旅行
ユーザー nayutanayuta
提出日時 2019-08-25 15:43:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 2,779 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 175,476 KB
実行使用メモリ 10,660 KB
最終ジャッジ日時 2023-08-07 19:57:34
合計ジャッジ時間 3,408 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
10,660 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,504 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 18 ms
5,132 KB
testcase_12 AC 23 ms
5,392 KB
testcase_13 AC 30 ms
6,364 KB
testcase_14 AC 13 ms
4,376 KB
testcase_15 AC 28 ms
6,192 KB
testcase_16 AC 45 ms
7,816 KB
testcase_17 AC 32 ms
6,956 KB
testcase_18 AC 18 ms
4,908 KB
testcase_19 AC 16 ms
4,700 KB
testcase_20 AC 10 ms
4,380 KB
testcase_21 AC 37 ms
7,128 KB
testcase_22 AC 34 ms
7,452 KB
testcase_23 AC 10 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 47 ms
7,828 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<long long, long long> Pi;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const long long INF = (long long)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

vector<long long> dijkstra(vector<vector<Pi>>& G, int s) {
  vector<long long> dist(G.size(), INF);
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  dist[s] = 0;
  que.emplace(dist[s], s);  // cost, to
  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    long long cost = p.first;
    int cur = p.second;
    if (dist[cur] < cost) continue;
    for (auto nv : G[cur]) {
      long long next_cost = dist[cur] + nv.second;
      if (dist[nv.first] <= next_cost) continue;
      dist[nv.first] = next_cost;
      que.emplace(dist[nv.first], nv.first);
    }
  }

  return dist;
}

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

  lint N, M, P, Q, T;
  cin >> N >> M >> P >> Q >> T;
  P--;
  Q--;
  vector<vector<Pi>> G(N, vector<Pi>());

  lint a, b, c;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(make_pair(b, c));
    G[b].push_back(make_pair(a, c));
  }

  vector<lint> dist = dijkstra(G, 0);
  vector<lint> Pdist = dijkstra(G, P);
  vector<lint> Qdist = dijkstra(G, Q);

  if (dist[P] + Pdist[Q] + Qdist[0] <= T) {
    cout << T << endl;
    return 0;
  }

  lint res = -1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      lint div = max(Pdist[i] + Pdist[j], Qdist[i] + Qdist[j]);
      if (dist[i] + div + dist[j] > T) continue;
      res = max(res, T - div);
    }
  }

  cout << res << endl;

  return 0;
}
0