結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー SHIJOUSHIJOU
提出日時 2021-04-02 22:43:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 468 ms / 3,000 ms
コード長 3,579 bytes
コンパイル時間 2,470 ms
コンパイル使用メモリ 225,004 KB
実行使用メモリ 48,164 KB
最終ジャッジ日時 2023-08-25 12:56:22
合計ジャッジ時間 13,177 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 7 ms
4,380 KB
testcase_03 AC 30 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 30 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 27 ms
4,380 KB
testcase_09 AC 11 ms
5,352 KB
testcase_10 AC 32 ms
4,464 KB
testcase_11 AC 34 ms
4,624 KB
testcase_12 AC 397 ms
42,000 KB
testcase_13 AC 177 ms
33,572 KB
testcase_14 AC 251 ms
38,704 KB
testcase_15 AC 220 ms
35,916 KB
testcase_16 AC 300 ms
38,072 KB
testcase_17 AC 455 ms
43,004 KB
testcase_18 AC 449 ms
43,048 KB
testcase_19 AC 367 ms
41,632 KB
testcase_20 AC 451 ms
42,884 KB
testcase_21 AC 468 ms
42,964 KB
testcase_22 AC 186 ms
39,008 KB
testcase_23 AC 375 ms
48,164 KB
testcase_24 AC 159 ms
34,492 KB
testcase_25 AC 312 ms
43,812 KB
testcase_26 AC 150 ms
38,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

template<class T>
struct lca {
  int n, root, l;
  vector<vector<int> > to, par;
  vector<vector<T> > co;
  vector<int> dep;
  vector<T> costs;
  lca(int n):n(n), to(n), co(n), dep(n), costs(n) {
    l = 0;
    while((1<<l) < n) ++l;
    par = vector<vector<int> >(n+1, vector<int>(l, n));
  }
  void add(int a, int b, T c=0) {
    to[a].push_back(b); co[a].push_back(c);
    to[b].push_back(a); co[b].push_back(c);
  }
  void dfs(int v, int d=0, T c=0, int p=-1) {
    if(p != -1) par[v][0] = p;
    dep[v] = d;
    costs[v] = c;
    for(int i = 0; i < to[v].size(); ++i) {
      int u = to[v][i];
      if(u == p) continue;
      dfs(u, d+1, c+co[v][i], v);
    }
  }
  void init(int _root=0) {
    root = _root;
    dfs(root);
    for(int i = 0; i < l-1; ++i) {
      for(int v = 0; v < n; ++v) {
        par[v][i+1] = par[par[v][i]][i];
      }
    }
  }
  int lca_(int a, int b) {
    if(dep[a] > dep[b]) swap(a, b);
    int gap = dep[b]-dep[a];
    for(int i = l-1; i >= 0; --i) {
      int len = 1<<i;
      if(gap >= len) {
        gap -= len;
        b = par[b][i];
      }
    }
    if(a == b) return a;
    for(int i = l-1; i >= 0; --i) {
      int na = par[a][i];
      int nb = par[b][i];
      if(na != nb) {
        a = na;
        b = nb;
      }
    }
    return par[a][0];
  }
  int length(int a, int b) {
    int c = lca_(a, b);
    return dep[a]+dep[b]-dep[c]*2;
  }
  T dist(int a, int b) {
    int c = lca_(a, b);
    return costs[a]+costs[b]-costs[c]*2;
  }
};

//head



int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<vector<pair<int, ll>>> G(n+k);
  lca<ll> l(n);
  rep(i, n-1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
    l.add(a, b, c);
  }
  l.init();
  vi _(k);
  rep(i, k) {
    int m, p;
    cin >> m >> p;
    _[i] = p;
    rep(j, m) {
      int x;
      cin >> x;
      x--;
      G[x].emplace_back(n+i, p);
      G[n+i].emplace_back(x, 0);
    }
  }
  PQG<pair<ll, int>> que;
  vector<vector<ll>> dist(k, vector<ll>(n+k, LINF));
  rep(i, k) {
    que.emplace(0, n+i);
    dist[i][n+i] = 0;
    while(not que.empty()) {
      auto [d, now] = que.top();
      que.pop();
      if(dist[i][now] != d) continue;
      for(auto [to, c]:G[now]) {
        if(chmin(dist[i][to], d+c)) {
          que.emplace(d+c, to);
        }
      }
    }
  }
  int q;
  cin >> q;
  while(q--) {
    int u, v;
    cin >> u >> v;
    u--; v--;
    ll ans = l.dist(u, v);
    rep(i, k) chmin(ans, dist[i][u]+dist[i][v]+_[i]);
    cout << ans << '\n';
  }
}
0