結果
| 問題 |
No.1442 I-wate Shortest Path Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-04-02 22:26:50 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,587 bytes |
| コンパイル時間 | 2,845 ms |
| コンパイル使用メモリ | 224,556 KB |
| 最終ジャッジ日時 | 2025-01-20 09:35:21 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 WA * 1 MLE * 4 |
ソースコード
//#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);
vvi mg(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]--;
}
sort(all(x[i]));
rep(j, i) {
int k = 0;
for(auto z:x[i]) {
while(k < m[j] and x[j][k] < z) k++;
if(x[j][k] == z) {
mg[i].emplace_back(j);
mg[j].emplace_back(i);
break;
}
}
}
}
vector<vector<ll>> dist(1<<k, vector<ll>(n, LINF));
vector<ll> su(1<<k);
PQG<pair<ll, int>> q;
for(int i = 1; i < 1<<k; i++) {
int u = __builtin_ctz(i);
su[i] = p[u];
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 {
vi lst;
rep(j, k) if(i>>j&1) lst.emplace_back(j);
int siz = size(lst);
vi cnt(siz);
rep(j, siz) {
int x = 0;
for(auto ne:mg[lst[j]]) {
while(x < siz and lst[x] < ne) x++;
if(lst[x] == ne) cnt[ne]++;
}
}
rep(j, siz) if(cnt[j] <= 1) {
u = lst[j];
break;
}
su[i] = p[u]+su[i^(1<<u)];
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);
}
}
int qq;
cin >> qq;
while(qq--) {
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';
}
}