結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー SSRSSSRS
提出日時 2021-03-26 22:30:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 749 ms / 3,000 ms
コード長 2,681 bytes
コンパイル時間 2,270 ms
コンパイル使用メモリ 194,140 KB
実行使用メモリ 37,240 KB
最終ジャッジ日時 2023-08-19 09:03:36
合計ジャッジ時間 12,694 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 13 ms
4,380 KB
testcase_03 AC 176 ms
4,380 KB
testcase_04 AC 14 ms
4,376 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 172 ms
4,376 KB
testcase_07 AC 10 ms
4,380 KB
testcase_08 AC 173 ms
4,380 KB
testcase_09 AC 23 ms
4,800 KB
testcase_10 AC 196 ms
4,380 KB
testcase_11 AC 182 ms
4,380 KB
testcase_12 AC 515 ms
32,648 KB
testcase_13 AC 310 ms
26,728 KB
testcase_14 AC 427 ms
31,044 KB
testcase_15 AC 374 ms
29,096 KB
testcase_16 AC 446 ms
31,600 KB
testcase_17 AC 625 ms
36,088 KB
testcase_18 AC 749 ms
36,796 KB
testcase_19 AC 563 ms
33,580 KB
testcase_20 AC 633 ms
35,944 KB
testcase_21 AC 632 ms
37,240 KB
testcase_22 AC 339 ms
28,268 KB
testcase_23 AC 569 ms
34,372 KB
testcase_24 AC 322 ms
27,316 KB
testcase_25 AC 489 ms
32,792 KB
testcase_26 AC 325 ms
29,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int LOG = 17;
const long long INF = 1000000000000000;
int main(){
  int N, K;
  cin >> N >> K;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++){
    int A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    C *= 2;
    E[A].push_back(make_pair(C, B));
    E[B].push_back(make_pair(C, A));
  }
  vector<vector<pair<int, int>>> E2 = E;
  for (int i = 0; i < K; i++){
    E2.push_back(vector<pair<int, int>>(0));
  }
  for (int i = 0; i < K; i++){
    int M, P;
    cin >> M >> P;
    for (int j = 0; j < M; j++){
      int X;
      cin >> X;
      X--;
      E2[X].push_back(make_pair(P, N + i));
      E2[N + i].push_back(make_pair(P, X));
    }
  }
  vector<vector<long long>> d(K, vector<long long>(N + K, -1));
  for (int i = 0; i < K; i++){
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
    pq.push(make_pair(0, N + i));
    while (!pq.empty()){
      long long dd = pq.top().first;
      int v = pq.top().second;
      pq.pop();
      if (d[i][v] == -1){
        d[i][v] = dd;
        for (auto P : E2[v]){
          int w = P.second;
          if (d[i][w] == -1){
            pq.push(make_pair(dd + P.first, w));
          }
        }
      }
    }
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<long long> d1(N, 0);
  vector<int> d2(N, 0);
  queue<int> q;
  q.push(0);
  while (!q.empty()){
    int v = q.front();
   q.pop();
    for (auto P : E[v]){
      int w = P.second;
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        d1[w] = d1[v] + P.first;
        d2[w] = d2[v] + 1;
        q.push(w);
      }
    }
  }
  vector<vector<int>> pp(LOG, vector<int>(N, -1));
  pp[0] = p;
  for (int i = 0; i < LOG - 1; i++){
    for (int j = 0; j < N; j++){
      if (pp[i][j] != -1){
        pp[i + 1][j] = pp[i][pp[i][j]];
      }
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++){
    int U, V;
    cin >> U >> V;
    U--;
    V--;
    long long ans = INF;
    for (int j = 0; j < K; j++){
      for (int k = 0; k < K; k++){
        ans = min(ans, d[j][U] + d[k][V] + d[j][N + k]);
      }
    }
    int u = U, v = V;
    if (d2[U] > d2[V]){
      swap(U, V);
    }
    for (int j = 0; j < LOG; j++){
      if (((d2[V] - d2[U]) >> j & 1) == 1){
        V = pp[j][V];
      }
    }
    int w;
    if (U == V){
      w = U;
    } else {
      for (int i = LOG - 1; i >= 0; i--){
        if (pp[i][U] != pp[i][V]){
          U = pp[i][U];
          V = pp[i][V];
        }
      }
      w = p[U];
    }
    ans = min(ans, d1[u] + d1[v] - 2 * d1[w]);
    ans /= 2;
    cout << ans << endl;
  }
}
0