結果

問題 No.848 なかよし旅行
ユーザー simansiman
提出日時 2021-06-03 09:34:30
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 1,931 bytes
コンパイル時間 3,483 ms
コンパイル使用メモリ 141,708 KB
実行使用メモリ 42,956 KB
最終ジャッジ日時 2024-04-28 03:24:57
合計ジャッジ時間 5,867 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 203 ms
42,804 KB
testcase_01 AC 12 ms
35,200 KB
testcase_02 AC 11 ms
35,072 KB
testcase_03 AC 12 ms
35,072 KB
testcase_04 AC 11 ms
35,200 KB
testcase_05 AC 12 ms
35,184 KB
testcase_06 AC 12 ms
35,192 KB
testcase_07 AC 10 ms
34,944 KB
testcase_08 AC 12 ms
35,200 KB
testcase_09 AC 14 ms
35,328 KB
testcase_10 AC 13 ms
35,200 KB
testcase_11 AC 79 ms
36,988 KB
testcase_12 AC 121 ms
38,484 KB
testcase_13 AC 162 ms
39,240 KB
testcase_14 AC 59 ms
35,584 KB
testcase_15 AC 146 ms
39,076 KB
testcase_16 AC 256 ms
42,956 KB
testcase_17 AC 165 ms
39,524 KB
testcase_18 AC 76 ms
36,948 KB
testcase_19 AC 74 ms
36,736 KB
testcase_20 AC 15 ms
35,360 KB
testcase_21 AC 207 ms
42,236 KB
testcase_22 AC 206 ms
42,952 KB
testcase_23 AC 47 ms
35,304 KB
testcase_24 AC 12 ms
35,172 KB
testcase_25 AC 263 ms
39,872 KB
testcase_26 AC 10 ms
35,024 KB
testcase_27 AC 12 ms
35,116 KB
testcase_28 AC 11 ms
35,072 KB
testcase_29 AC 11 ms
35,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 2010;
ll D[MAX_N][MAX_N];
vector<int> E[MAX_N];

struct Node {
  int v;
  ll dist;

  Node(int v = -1, ll dist = -1) {
    this->v = v;
    this->dist = dist;
  }

  bool operator>(const Node &n) const {
    return dist > n.dist;
  }
};

void set_dist(int v) {
  priority_queue <Node, vector<Node>, greater<Node>> pque;
  pque.push(Node(v, 0));
  bool visited[MAX_N + 1];
  memset(visited, false, sizeof(visited));

  while (not pque.empty()) {
    Node node = pque.top();
    pque.pop();

    if (visited[node.v]) continue;
    visited[node.v] = true;
    D[v][node.v] = node.dist;
    D[node.v][v] = node.dist;

    for (int u : E[node.v]) {
      pque.push(Node(u, node.dist + D[node.v][u]));
    }
  }
}

int main() {
  int N, M, P, Q, T;
  cin >> N >> M >> P >> Q >> T;
  memset(D, 0, sizeof(D));

  ll a, b, c;
  for (int i = 0; i < M; ++i) {
    cin >> a >> b >> c;

    E[a].push_back(b);
    E[b].push_back(a);
    D[a][b] = c;
    D[b][a] = c;
  }

  set_dist(1);
  set_dist(P);
  set_dist(Q);

  ll d1 = D[1][P];
  ll d2 = D[P][Q];
  ll d3 = D[Q][1];
  ll bd = max(d1, d3);
  ll td = d1 + d2 + d3;

  fprintf(stderr, "d1: %lld, d2: %lld, d3(%d -> %d): %lld, td: %lld\n",
      d1, d2, Q, 1, d3, td);

  if (td <= T) {
    cout << T << endl;
  } else if (2 * bd > T) {
    cout << -1 << endl;
  } else {
    ll max_d = 0;

    for (int i = 1; i <= N; ++i) {
      ll fd = D[1][i];

      for (int j = 1; j <= N; ++j) {
        ll d = max(D[i][P] + D[P][j], D[i][Q] + D[Q][j]);

        ll rd = D[j][1];
        if (fd + d + rd > T) continue;

        ll t = T - d;
        max_d = max(max_d, t);
      }
    }

    cout << max_d << endl;
  }

  return 0;
}
0