結果

問題 No.1833 Subway Planning
ユーザー SSRSSSRS
提出日時 2022-02-04 21:49:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,815 ms / 4,000 ms
コード長 2,362 bytes
コンパイル時間 4,279 ms
コンパイル使用メモリ 214,144 KB
実行使用メモリ 20,424 KB
最終ジャッジ日時 2023-09-02 04:44:29
合計ジャッジ時間 23,756 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 340 ms
18,740 KB
testcase_05 AC 482 ms
18,792 KB
testcase_06 AC 457 ms
18,752 KB
testcase_07 AC 1,592 ms
18,684 KB
testcase_08 AC 1,709 ms
18,812 KB
testcase_09 AC 2,815 ms
18,628 KB
testcase_10 AC 1,054 ms
20,284 KB
testcase_11 AC 709 ms
20,288 KB
testcase_12 AC 460 ms
20,424 KB
testcase_13 AC 603 ms
20,400 KB
testcase_14 AC 421 ms
19,936 KB
testcase_15 AC 556 ms
19,744 KB
testcase_16 AC 600 ms
19,160 KB
testcase_17 AC 1,934 ms
19,156 KB
testcase_18 AC 1,730 ms
18,324 KB
testcase_19 AC 884 ms
19,144 KB
testcase_20 AC 665 ms
16,076 KB
testcase_21 AC 1,199 ms
19,568 KB
testcase_22 AC 810 ms
19,412 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int main(){
  int N;
  cin >> N;
  vector<tuple<int, int, int>> E(N - 1);
  vector<vector<int>> E2(N);
  for (int i = 0; i < N - 1; i++){
    int A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    E[i] = make_tuple(C, A, B);
    E2[A].push_back(B);
    E2[B].push_back(A);
  }
  int tv = INF, fv = -1;
  while (tv - fv > 1){
    int mid = (tv + fv) / 2;
    vector<bool> T(N, false);
    int a = -1;
    for (int i = 0; i < N - 1; i++){
      if (get<0>(E[i]) > mid){
        T[get<1>(E[i])] = true;
        T[get<2>(E[i])] = true;
        a = get<1>(E[i]);
      }
    }
    if (a == -1){
      tv = mid;
    } else {
      vector<int> d1(N, -1);
      d1[a] = 0;
      queue<int> Q1;
      Q1.push(a);
      int s;
      while (!Q1.empty()){
        int v = Q1.front();
        Q1.pop();
        if (T[v]){
          s = v;
        }
        for (int w : E2[v]){
          if (d1[w] == -1){
            d1[w] = d1[v] + 1;
            Q1.push(w);
          }
        }
      }
      vector<int> d2(N, -1);
      d2[s] = 0;
      queue<int> Q2;
      Q2.push(s);
      int t;
      while (!Q2.empty()){
        int v = Q2.front();
        Q2.pop();
        if (T[v]){
          t = v;
        }
        for (int w : E2[v]){
          if (d2[w] == -1){
            d2[w] = d2[v] + 1;
            Q2.push(w);
          }
        }
      }
      vector<int> d3(N, -1);
      d3[t] = 0;
      queue<int> Q3;
      Q3.push(t);
      while (!Q3.empty()){
        int v = Q3.front();
        Q3.pop();
        for (int w : E2[v]){
          if (d3[w] == -1){
            d3[w] = d3[v] + 1;
            Q3.push(w);
          }
        }
      }
      int D = d2[t];
      bool ok = true;
      for (int i = 0; i < N; i++){
        if (T[i]){
          if (d2[i] + d3[i] > D){
            ok = false;
          }
        }
      }
      int mn = INF;
      for (int i = 0; i < N - 1; i++){
        int A = get<1>(E[i]);
        int B = get<2>(E[i]);
        if (d2[A] + d3[B] == D - 1 || d2[B] + d3[A] == D - 1){
          mn = min(mn, get<0>(E[i]));
        }
      }
      for (int i = 0; i < N - 1; i++){
        if (get<0>(E[i]) - mn > mid){
          ok = false;
        }
      }
      if (ok){
        tv = mid;
      } else {
        fv = mid;
      }
    }
  }
  cout << tv << endl;
}
0