結果

問題 No.848 なかよし旅行
ユーザー simansiman
提出日時 2021-06-03 09:28:18
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,909 bytes
コンパイル時間 5,803 ms
コンパイル使用メモリ 105,380 KB
実行使用メモリ 35,428 KB
最終ジャッジ日時 2023-08-10 09:52:25
合計ジャッジ時間 5,510 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 12 ms
35,224 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 14 ms
35,268 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 46 ms
35,328 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 11 ms
35,200 KB
testcase_27 AC 9 ms
35,268 KB
testcase_28 AC 11 ms
35,196 KB
testcase_29 AC 9 ms
35,204 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 < N; ++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: %lld, td: %lld\n", d1, d2, d3, td);

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

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

      for (int j = 2; 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