結果

問題 No.848 なかよし旅行
ユーザー simansiman
提出日時 2021-06-03 09:34:30
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,931 bytes
コンパイル時間 4,966 ms
コンパイル使用メモリ 107,844 KB
実行使用メモリ 43,732 KB
最終ジャッジ日時 2023-08-10 09:59:00
合計ジャッジ時間 4,664 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 187 ms
42,788 KB
testcase_01 AC 11 ms
35,176 KB
testcase_02 AC 11 ms
35,228 KB
testcase_03 AC 10 ms
35,192 KB
testcase_04 AC 10 ms
35,212 KB
testcase_05 AC 11 ms
35,192 KB
testcase_06 AC 10 ms
35,288 KB
testcase_07 AC 10 ms
35,272 KB
testcase_08 AC 11 ms
35,316 KB
testcase_09 AC 12 ms
35,332 KB
testcase_10 AC 12 ms
35,312 KB
testcase_11 AC 79 ms
37,004 KB
testcase_12 AC 114 ms
38,396 KB
testcase_13 AC 153 ms
39,180 KB
testcase_14 AC 56 ms
35,504 KB
testcase_15 AC 144 ms
39,172 KB
testcase_16 AC 253 ms
43,732 KB
testcase_17 AC 161 ms
39,604 KB
testcase_18 AC 75 ms
36,916 KB
testcase_19 AC 69 ms
36,616 KB
testcase_20 AC 15 ms
35,280 KB
testcase_21 AC 198 ms
42,580 KB
testcase_22 AC 190 ms
43,648 KB
testcase_23 AC 47 ms
35,252 KB
testcase_24 AC 10 ms
35,172 KB
testcase_25 AC 247 ms
39,872 KB
testcase_26 AC 10 ms
35,256 KB
testcase_27 AC 10 ms
35,260 KB
testcase_28 AC 10 ms
35,360 KB
testcase_29 AC 12 ms
35,252 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