結果
| 問題 |
No.1442 I-wate Shortest Path Problem
|
| コンテスト | |
| ユーザー |
NOSS
|
| 提出日時 | 2021-03-26 23:31:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,809 bytes |
| コンパイル時間 | 3,208 ms |
| コンパイル使用メモリ | 225,372 KB |
| 最終ジャッジ日時 | 2025-01-19 23:37:15 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 1 |
| other | AC * 5 WA * 20 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using P = pair<int, int>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr int INF32 = 1 << 30;
constexpr ll INF64 = 1LL << 62;
// constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-10;
const double PI = acos(-1);
#define ALL(v) (v).begin(),(v).end()
#define REP(i,n) for(int i=0,i_len=n; i<i_len; ++i)
template<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }
template <typename Monoid>
struct SegmentTree{
private:
using F = std::function<Monoid(Monoid, Monoid)>;
int N;
std::vector<Monoid> node;
F f;
Monoid e; // identity element
public:
SegmentTree(){}
SegmentTree(F f, Monoid e):f(f), e(e){}
void init(int sz){
N = 1;
while(N < sz) N <<= 1;
node.assign(2*N-1, e);
}
void build(std::vector<Monoid>& v){
int sz = int(v.size());
init(sz);
for(int i=0; i<sz; i++){
node[i+N-1] = v[i];
}
for(int i=N-2; i>=0; i--){
node[i] = f(node[i*2+1], node[i*2+2]);
}
}
void update(int k, Monoid x){
k += N-1;
node[k] = x;
while(k > 0){
k = (k-1)/2;
node[k] = f(node[2*k+1], node[2*k+2]);
}
}
// [a,b)
Monoid query(int a, int b){return query(a, b, 0, 0, N);}
Monoid query(int a, int b, int k, int l, int r){
if(b <= l || r <= a) return e;
if(a <= l && r <= b) return node[k];
Monoid vl, vr;
vl = query(a, b, 2*k+1, l, (l+r)/2);
vr = query(a, b, 2*k+2, (l+r)/2, r);
return f(vl, vr);
}
};
template <typename T>
struct LCA {
using P = std::pair<int,int>;
using CostType = T;
const int INF = 1<<30;
struct edge {
int from, to, rev;
CostType cost;
edge(int from, int to, int rev, CostType cost) : from(from), to(to), rev(rev), cost(cost){}
};
int V = 0;
int root = 0;
std::vector<std::vector<edge> > graph;
std::vector<int> depth, vs, ds, us; // ds[v]:go down to v, us[v]:go up from v
private:
SegmentTree<P> rmq = SegmentTree<P>([](P a, P b){return min(a,b);},P(INF,-1));
SegmentTree<CostType> rsq = SegmentTree<CostType>([](CostType a, CostType b){return a+b;}, 0);
void dfs(int v, int p, int d, int &idx){
vs[idx] = v;
depth[v] = d;
ds[v] = idx++;
for(auto& e : graph[v]){
if(e.to == p) continue;
dfs(e.to, v, d+1, idx);
vs[idx] = v;
idx++;
}
us[v] = idx;
}
public:
LCA() = default;
LCA(int V, int root = 0) : V(V), graph(V), depth(V), vs(V*2-1), ds(V), us(V), root(root){}
void init(int n, int r = 0){
V = n;
graph.resize(V);
depth.resize(V);
vs.resize(V*2-1);
ds.resize(V);
us.resize(V);
root = r;
}
void add_edge(int from, int to, CostType cost = 1){
graph[from].emplace_back(edge(from,to,int(graph[to].size()),cost));
graph[to].emplace_back(edge(to,from,int(graph[from].size())-1,cost));
}
void build(){
int idx = 0;
dfs(root, -1, 0, idx);
std::vector<P> depv(idx);
for(int i=0;i<idx;i++){
depv[i] = P(depth[vs[i]], vs[i]);
}
rmq.build(depv);
std::vector<CostType> cstv(idx, 0);
for(int i=0;i<V;i++){
for(auto& e : graph[i]){
if(depth[e.from] < depth[e.to]){
cstv[ds[e.to]] = e.cost;
cstv[us[e.to]] = -e.cost;
}
}
}
rsq.build(cstv);
}
int query(int u, int v){
return rmq.query(std::min(ds[u],ds[v]), std::max(ds[u],ds[v])+1).second;
}
CostType dist(int u){
return rsq.query(ds[root], ds[u]+1);
}
CostType dist(int u, int v){
int w = query(u, v);
return dist(u) + dist(v) - 2*dist(w);
}
void update(int v, CostType cost){
rsq.update(ds[v], cost);
rsq.update(us[v], -cost);
}
};
int solve(){
int N, K;
cin >> N >> K;
LCA<ll> lca(N);
REP(i,N-1){
ll a, b, c;
cin >> a >> b >> c;
a--;b--;
lca.add_edge(a,b,c);
}
lca.build();
vector<ll> p(K);
vector<vector<ll> > ardt(K,vector<ll>(N,INF64));
REP(i,K){
int m;
cin >> m >> p[i];
queue<pair<int,ll> > que;
REP(j,m){
int x;
cin >> x;
x--;
que.push(make_pair(x,0));
ardt[i][x] = 0;
}
while(!que.empty()){
auto p = que.front();
que.pop();
int now = p.first;
if(ardt[i][now] < p.second) continue;
for(auto e : lca.graph[now]){
if(chmin(ardt[i][e.to], ardt[i][now]+e.cost)){
que.push(make_pair(e.to,ardt[i][e.to]));
}
}
}
}
int Q;
cin >> Q;
REP(i,Q){
int u, v;
cin >> u >> v;
u--; v--;
ll res = lca.dist(u,v);
REP(j,K){
chmin(res, ardt[j][u]+ardt[j][v]+p[j]);
}
cout << res << endl;
}
return 0;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
// int t; cin >> t; for(int i=0;i<t;i++) solve();
// while(!solve());
solve();
return 0;
}
NOSS