結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー outlineoutline
提出日時 2021-03-28 01:38:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 348 ms / 3,000 ms
コード長 5,238 bytes
コンパイル時間 1,975 ms
コンパイル使用メモリ 157,916 KB
実行使用メモリ 28,068 KB
最終ジャッジ日時 2023-08-19 15:02:05
合計ジャッジ時間 7,559 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 31 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 31 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 28 ms
4,376 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 33 ms
4,380 KB
testcase_11 AC 32 ms
4,380 KB
testcase_12 AC 205 ms
23,284 KB
testcase_13 AC 104 ms
18,532 KB
testcase_14 AC 159 ms
20,920 KB
testcase_15 AC 159 ms
20,580 KB
testcase_16 AC 221 ms
22,936 KB
testcase_17 AC 344 ms
26,952 KB
testcase_18 AC 341 ms
26,968 KB
testcase_19 AC 241 ms
23,424 KB
testcase_20 AC 348 ms
26,932 KB
testcase_21 AC 347 ms
26,960 KB
testcase_22 AC 117 ms
23,184 KB
testcase_23 AC 226 ms
28,068 KB
testcase_24 AC 93 ms
19,180 KB
testcase_25 AC 200 ms
26,144 KB
testcase_26 AC 103 ms
22,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template<typename T>
struct Edge{
    int to;
    T cost;
    Edge() = default;
    Edge(int to, T cost) : to(to), cost(cost) {}
};

template<typename T>
struct CostLCA{
    using G = vector<vector<Edge<T>>>;

    const int ub_log;
    vector<int> depth;
    vector<T> costs;
    const G& g;
    vector<vector<int>> table;

    CostLCA(const G& g) : g(g), depth(g.size()), costs(g.size()), ub_log(32 - __builtin_clz(g.size())){
        table.assign(ub_log, vector<int>(g.size(), -1));
    }

    void dfs(int from, int par = -1, int dep = 0, T sum = 0){
        table[0][from] = par;
        depth[from] = dep;
        costs[from] = sum;
        for(int i = 0; i < (int)g[from].size(); ++i){
            int to = g[from][i].to;
            T cost = g[from][i].cost;
            if(to != par)   dfs(to, from, dep + 1, sum + cost);
        }
    }

    void build(int root = 0){
        dfs(root);
        for(int k = 0; k + 1 < ub_log; ++k){
            for(int i = 0; i < (int)table[k].size(); ++i){
                if(table[k][i] == -1)   table[k + 1][i] = -1;
                else    table[k + 1][i] = table[k][table[k][i]];
            }
        }
    }

    int query(int u, int v){
        if(depth[u] > depth[v]) swap(u, v);
        
        v = get(v, depth[v] - depth[u]);
        if(u == v)  return u;

        for(int i = ub_log - 1; i >= 0; --i){
            if(table[i][u] != table[i][v]){
                u = table[i][u];
                v = table[i][v];
            }
        }
        return table[0][u];
    }

    int get(int v, int x){
        if(x <= 0)  return v;
        for(int i = ub_log - 1; i >= 0; --i){
            if(x >> i & 1)  v = table[i][v];
        }
        return v;
    }

    int length(int u, int v){
        int lca = query(u, v);
        return depth[u] + depth[v] - depth[lca] * 2;
    }

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

    void print(){
        for(int k = 0; k + 1 < ub_log; ++k){
            for(int i = 0; i < (int)table[k].size(); ++i){
                cerr << table[k][i] << " ";
            }
            cerr << "\n";
        }
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K, Q;
    cin >> N >> K;
    vector<vector<Edge<ll>>> g(N);
    for(int i = 1; i < N; ++i){
        int A, B, C;
        cin >> A >> B >> C;
        --A, --B;
        g[A].emplace_back(B, C);
        g[B].emplace_back(A, C);
    }
    CostLCA<ll> lca(g);
    lca.build();
    vector<int> M(K), P(K);
    vector<vector<int>> X(K);
    for(int i = 0; i < K; ++i){
        cin >> M[i] >> P[i];
        X[i].resize(M[i]);
        for(int j = 0; j < M[i]; ++j){
            cin >> X[i][j];
            --X[i][j];
        }
    }

    constexpr ll inf = 1e+17;
    vector<vector<ll>> dist(K, vector<ll>(N, inf));
    vector<vector<ll>> dp(K, vector<ll>(K, inf));
    for(int i = 0; i < K; ++i){
        dp[i][i] = P[i];
        using node = pair<ll, int>;
        priority_queue<node, vector<node>, greater<node>> que;
        for(int j = 0; j < M[i]; ++j){
            dist[i][X[i][j]] = 0;
            que.emplace(0, X[i][j]);
        }
        while(!que.empty()){
            auto [d, from] = que.top();
            que.pop();
            if(d > dist[i][from])   continue;
            for(auto& [to, cost] : g[from]){
                if(chmin(dist[i][to], d + cost)){
                    que.emplace(dist[i][to], to);
                }
            }
        }
        for(int j = 0; j < K; ++j){
            if(j == i)  continue;
            for(int k = 0; k < M[j]; ++k){
                chmin(dp[i][j], dist[i][X[j][k]] + P[i] + P[j]);
                chmin(dp[j][i], dist[i][X[j][k]] + P[i] + P[j]);
            }
        }
    }

    for(int k = 0; k < K; ++k){
        for(int i = 0; i < K; ++i){
            for(int j = 0; j < K; ++j){
                chmin(dp[i][j], dp[i][k] + dp[k][j] - P[k]);
            }
        }
    }

    cin >> Q;
    for(int q = 0; q < Q; ++q){
        int U, V;
        cin >> U >> V;
        --U, --V;
        ll ans = lca.dist(U, V);
        for(int i = 0; i < K; ++i){
            for(int j = 0; j < K; ++j){
                chmin(ans, dist[i][U] + dp[i][j] + dist[j][V]);
            }
        }
        cout << ans << '\n';
    }

    return 0;
}
0