結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー SHIJOUSHIJOU
提出日時 2021-04-02 20:10:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,942 bytes
コンパイル時間 4,436 ms
コンパイル使用メモリ 224,128 KB
実行使用メモリ 811,792 KB
最終ジャッジ日時 2023-08-25 04:21:48
合計ジャッジ時間 11,163 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 7 ms
4,384 KB
testcase_03 WA -
testcase_04 AC 6 ms
4,392 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 32 ms
4,380 KB
testcase_09 AC 12 ms
5,472 KB
testcase_10 AC 48 ms
5,048 KB
testcase_11 AC 37 ms
4,540 KB
testcase_12 AC 471 ms
47,324 KB
testcase_13 AC 254 ms
34,184 KB
testcase_14 AC 331 ms
36,776 KB
testcase_15 AC 337 ms
37,348 KB
testcase_16 AC 546 ms
59,272 KB
testcase_17 MLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
  lca<ll> l(n);
  rep(i, n-1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    l.add(a, b, c);
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
  }
  l.init();
  vi m(k), p(k);
  vvi x(k);
  rep(i, k) {
    cin >> m[i] >> p[i];
    x[i].resize(m[i]);
    rep(j, m[i]) {
      cin >> x[i][j];
      x[i][j]--;
    }
  }
  vector<vector<ll>> dist(1<<k, vector<ll>(n, LINF));
  vector<ll> su(1<<k);
  for(int i = 1; i < 1<<k; i++) {
    PQG<pair<ll, int>> q;
    int u = __builtin_ctz(i);
    if(__builtin_popcount(i) == 1) {
      rep(j, m[u]) {
        dist[i][x[u][j]] = 0;
        q.emplace(0, x[u][j]);
      }
      while(not q.empty()) {
        auto [d, now] = q.top();
        q.pop();
        if(dist[i][now] != d) continue;
        for(auto [to, c]:G[now]) {
          if(chmin(dist[i][to], d+c)) {
            q.emplace(d+c, to);
          }
        }
      }
    }
    else {
      ll mi = LINF;
      rep(j, m[u]) chmin(mi, dist[i^(1<<u)][x[u][j]]);
      rep(j, n) dist[i][j] = min(dist[i^(1<<u)][j], dist[1<<u][j]+mi);
    }
    rep(j, k) if(i>>j&1) su[i] += p[j];
  }
  int q;
  cin >> q;
  while(q--) {
    int u, v;
    cin >> u >> v;
    u--; v--;
    ll ans = l.dist(u, v);
    for(int i = 1; i < 1<<k; i++) {
      chmin(ans, su[i]+dist[i][u]+dist[i][v]);
    }
    cout << ans << '\n';
  }
}
0