結果

問題 No.848 なかよし旅行
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-07-13 14:59:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,104 bytes
コンパイル時間 1,097 ms
コンパイル使用メモリ 90,612 KB
実行使用メモリ 10,592 KB
最終ジャッジ日時 2024-04-25 13:56:29
合計ジャッジ時間 2,315 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
10,592 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 11 ms
6,944 KB
testcase_12 AC 13 ms
6,944 KB
testcase_13 AC 16 ms
6,940 KB
testcase_14 AC 9 ms
6,944 KB
testcase_15 AC 14 ms
6,944 KB
testcase_16 AC 21 ms
8,576 KB
testcase_17 AC 16 ms
7,552 KB
testcase_18 AC 11 ms
6,944 KB
testcase_19 AC 10 ms
6,944 KB
testcase_20 AC 8 ms
6,940 KB
testcase_21 AC 17 ms
7,552 KB
testcase_22 AC 12 ms
7,552 KB
testcase_23 AC 18 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 25 ms
8,448 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using i64 = int64_t;

struct edge { int to; i64 cost; };
constexpr i64 inf = 987'654'321'987'654'321LL;

template <class T>
inline void readint(T *x) {
  *x = 0;
  int c;
  for(;;) {
    c = getchar_unlocked();
    switch(c) case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: goto num;
  }

num:
  for(;;) {
    switch(c) {
      case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
        *x *= 10;
        *x += c - 48;
        break;
      default:
        return;
    }
    c = getchar_unlocked();
  }
}

int N;
vector<vector<edge>> G;

vector<i64> dijk(int s) {
  vector<i64> cost(N, inf);
  cost[s] = 0;
  priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> pq;
  pq.emplace(0, s);
  vector<bool> seen(N, false);
  while(!pq.empty()) {
    i64 _; int v; tie(_, v) = pq.top(); pq.pop();
    if(seen[v]) { continue; }
    seen[v] = true;
    for(edge &e : G[v]) {
      if(cost[e.to] > cost[v] + e.cost) {
        cost[e.to] = cost[v] + e.cost;
        pq.emplace(cost[e.to], e.to);
      }
    }
  }
  return cost;
}

int main(void) {
  int M, P, Q; i64 T;
  readint(&N);
  readint(&M);
  readint(&P);
  readint(&Q);
  readint(&T);
  G.assign(N, vector<edge>());
  --P; --Q;
  for(int i=0; i<M; ++i) {
    int a, b, c;
    readint(&a);
    readint(&b);
    readint(&c);
    --a; --b;
    G[a].push_back(edge{b, c});
    G[b].push_back(edge{a, c});
  }
  vector<i64> c0 = dijk(0),
              c1 = dijk(P),
              c2 = dijk(Q);

  // 0 -> P -> Q -> 0
  if(c0[P] + c1[Q] + c0[Q] <= T) {
    printf("%ld\n", T);
    return 0;
  }

  // 0 -> x -> P -> y -> 0
  //           Q
  i64 maxi = -1;
  for(int x=0; x<N; ++x) {
    for(int y=0; y<N; ++y) {
      i64 r1 = c0[x] + c1[x] + c1[y] + c0[y],
          r2 = c0[x] + c2[x] + c2[y] + c0[y];
      i64 rr = max(r1, r2);
      if(rr <= T) {
        maxi = max(maxi, T - rr + c0[x] + c0[y]);
      }
    }
  }
  printf("%ld\n", maxi);
  return 0;
}
0