結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tokusakuraitokusakurai
提出日時 2021-03-26 23:05:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,897 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 232,136 KB
実行使用メモリ 46,740 KB
最終ジャッジ日時 2024-11-29 01:13:59
合計ジャッジ時間 9,064 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
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 WA -
testcase_23 WA -
testcase_24 AC 120 ms
28,656 KB
testcase_25 AC 290 ms
39,152 KB
testcase_26 AC 134 ms
36,872 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];
    }

    int 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