結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー NOSSNOSS
提出日時 2021-03-26 23:31:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,809 bytes
コンパイル時間 2,946 ms
コンパイル使用メモリ 231,204 KB
実行使用メモリ 30,772 KB
最終ジャッジ日時 2023-08-19 10:21:06
合計ジャッジ時間 14,897 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 10 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 395 ms
24,800 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 394 ms
28,968 KB
testcase_23 WA -
testcase_24 AC 384 ms
24,176 KB
testcase_25 WA -
testcase_26 AC 374 ms
26,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0