結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tokusakuraitokusakurai
提出日時 2021-03-26 23:06:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 584 ms / 3,000 ms
コード長 4,895 bytes
コンパイル時間 3,187 ms
コンパイル使用メモリ 229,116 KB
実行使用メモリ 46,564 KB
最終ジャッジ日時 2023-08-19 09:51:38
合計ジャッジ時間 10,489 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 7 ms
4,512 KB
testcase_03 AC 35 ms
4,376 KB
testcase_04 AC 7 ms
4,376 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 34 ms
4,384 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 31 ms
4,380 KB
testcase_09 AC 12 ms
5,472 KB
testcase_10 AC 38 ms
4,456 KB
testcase_11 AC 36 ms
4,460 KB
testcase_12 AC 381 ms
39,824 KB
testcase_13 AC 173 ms
29,700 KB
testcase_14 AC 246 ms
36,560 KB
testcase_15 AC 219 ms
32,280 KB
testcase_16 AC 315 ms
34,592 KB
testcase_17 AC 512 ms
39,440 KB
testcase_18 AC 487 ms
39,728 KB
testcase_19 AC 423 ms
38,944 KB
testcase_20 AC 584 ms
39,484 KB
testcase_21 AC 558 ms
39,612 KB
testcase_22 AC 189 ms
34,860 KB
testcase_23 AC 454 ms
46,564 KB
testcase_24 AC 140 ms
28,832 KB
testcase_25 AC 353 ms
39,420 KB
testcase_26 AC 142 ms
36,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

template<typename T, bool directed = false>
struct Weighted_Graph{
    struct edge{
        int to; T cost; int id;
        edge(int to, T cost, int id) : to(to), cost(cost), id(id) {}
    };

    vector<vector<edge>> es;
    vector<T> d;
    vector<int> pre_v;
    vector<int> keep;
    const T INF_T;
    const int n;
    int m;

    Weighted_Graph(int n) : es(n), d(n), pre_v(n), INF_T(numeric_limits<T>::max()/2), n(n), m(0) {}

    void add_edge(int from, int to, T cost){
        es[from].emplace_back(to, cost, m);
        if(!directed) es[to].emplace_back(from, cost, m);
        m++;
    }

    T dijkstra(int s, int t = 0){
        fill(begin(d), end(d), INF_T);
        using P = pair<T, int>;
        priority_queue<P, vector<P>, greater<P> > que;
        que.emplace(d[s] = 0, s);
        while(!que.empty()){
            auto [p, i] = que.top(); que.pop();
            if(p > d[i]) continue;
            for(auto &e: es[i]){
                if(d[i]+e.cost < d[e.to]){
                    pre_v[e.to] = i, que.emplace(d[e.to] = d[i]+e.cost, e.to);
                }
            }
        }
        return d[t];
    }

    vector<int> shortest_path(int s, int t){
        keep.clear();
        if(dijkstra(s, t) == INF_T) return keep;
        for(int now = t; now != s; now = pre_v[now]) keep.push_back(now);
        keep.push_back(s), reverse(begin(keep), end(keep));
        return keep;
    }
};

template<typename T, bool directed = false>
struct Weighted_Graph_2{
    struct edge{
        int to; T cost; int id;
        edge(int to, T cost, int id) : to(to), cost(cost), id(id) {}
    };

    vector<vector<edge>> es;
    vector<vector<int>> par; //par[i][j] := 頂点jの2^i個前の祖先
    vector<int> depth;
    vector<T> di;
    const T INF_T;
    const int n, height;
    int m;

    Weighted_Graph_2(int n) : es(n), depth(n), di(n), INF_T(numeric_limits<T>::max()/2), n(n), height(32-__builtin_clz(n)), m(0){
        par.assign(height, vector<int>(n));
    }

    void add_edge(int from, int to, T cost){
        es[from].emplace_back(to, cost, m);
        if(!directed) es[to].emplace_back(from, cost, m);
        m++;
    }

    void prepare(int now, int pre = -1){
        if(pre == -1) depth[now] = 0, di[now] = 0;
        par[0][now] = pre;
        for(auto &e: es[now]){
            if(e.to != pre){
                depth[e.to] = depth[now]+1, di[e.to] = di[now]+e.cost;
                prepare(e.to, now);
            }
        }
    }

    void build(int root = 0){
        prepare(root);
        for(int j = 0; j < height-1; j++){
            for(int i = 0; i < n; i++){
                if(par[j][i] == -1) par[j+1][i] = -1;
                else par[j+1][i] = par[j][par[j][i]];
            }
        }
    }

    int lca(int u, int v){
        if(depth[u] < depth[v]) swap(u, v);
        int D = depth[u]-depth[v];
        for(int i = 0; i < height; i++){
            if((D>>i)&1) u = par[i][u];
        }
        if(u == v) return u;
        for(int i = height-1; i >= 0; i--){
            if(par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
        }
        return par[0][u];
    }

    T dist(int u, int v){
        return di[u]+di[v]-2*di[lca(u, v)];
    }
};

int main(){
    int N, K; cin >> N >> K;

    Weighted_Graph<ll> G(N+K);
    Weighted_Graph_2<ll> G2(N);

    rep(i, N-1){
        int u, v; ll c; cin >> u >> v >> c; u--, v--;

        G.add_edge(u, v, c*2), G2.add_edge(u, v, c);
    }

    G2.build(0);

    rep(i, K){
        int M; ll c; cin >> M >> c;
        while(M--){
            int u; cin >> u; u--;
            G.add_edge(u, N+i, c);
        }
    }

    vector<vector<ll>> d(K, vector<ll>(N+K));

    rep(i, K){
        G.dijkstra(N+i);
        d[i] = G.d;
    }

    int Q; cin >> Q;

    while(Q--){
        int u, v; cin >> u >> v; u--, v--;

        ll ans = G2.dist(u, v);

        rep(i, K) chmin(ans, (d[i][u]+d[i][v])/2);

        cout << ans << '\n';
    }
}
0