結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tokusakuraitokusakurai
提出日時 2021-03-26 23:06:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 533 ms / 3,000 ms
コード長 4,895 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 230,192 KB
実行使用メモリ 46,616 KB
最終ジャッジ日時 2024-05-06 17:06:12
合計ジャッジ時間 9,873 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 6 ms
5,376 KB
testcase_03 AC 34 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 32 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 32 ms
5,376 KB
testcase_09 AC 12 ms
5,504 KB
testcase_10 AC 38 ms
5,376 KB
testcase_11 AC 35 ms
5,376 KB
testcase_12 AC 348 ms
40,020 KB
testcase_13 AC 152 ms
29,780 KB
testcase_14 AC 256 ms
36,676 KB
testcase_15 AC 203 ms
32,364 KB
testcase_16 AC 305 ms
34,668 KB
testcase_17 AC 519 ms
39,580 KB
testcase_18 AC 533 ms
39,800 KB
testcase_19 AC 381 ms
39,080 KB
testcase_20 AC 498 ms
39,540 KB
testcase_21 AC 533 ms
39,884 KB
testcase_22 AC 201 ms
34,924 KB
testcase_23 AC 383 ms
46,616 KB
testcase_24 AC 122 ms
28,664 KB
testcase_25 AC 337 ms
39,028 KB
testcase_26 AC 151 ms
36,744 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