結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー rniyarniya
提出日時 2021-03-26 23:08:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 481 ms / 3,000 ms
コード長 8,382 bytes
コンパイル時間 2,973 ms
コンパイル使用メモリ 229,740 KB
実行使用メモリ 44,316 KB
最終ジャッジ日時 2024-05-06 17:09:20
合計ジャッジ時間 9,815 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 33 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 32 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 28 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 36 ms
5,376 KB
testcase_11 AC 33 ms
5,376 KB
testcase_12 AC 313 ms
39,220 KB
testcase_13 AC 185 ms
30,600 KB
testcase_14 AC 243 ms
35,948 KB
testcase_15 AC 224 ms
33,304 KB
testcase_16 AC 293 ms
35,864 KB
testcase_17 AC 433 ms
40,836 KB
testcase_18 AC 445 ms
40,856 KB
testcase_19 AC 356 ms
38,860 KB
testcase_20 AC 443 ms
40,712 KB
testcase_21 AC 481 ms
40,820 KB
testcase_22 AC 207 ms
34,848 KB
testcase_23 AC 360 ms
44,316 KB
testcase_24 AC 160 ms
31,376 KB
testcase_25 AC 306 ms
40,468 KB
testcase_26 AC 144 ms
36,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U, typename V> ostream& operator<<(ostream& os, const tuple<T, U, V>& t) {
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';
    return os;
}
template <typename T, typename U, typename V, typename W> ostream& operator<<(ostream& os, const tuple<T, U, V, W>& t) {
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

/**
 * @brief Lowest Common Ancestor
 * @docs docs/tree/LowestCommonAncestor.md
 */
struct LowestCommonAncestor {
    int n, h;
    vector<vector<int>> G, par;
    vector<int> dep;
    LowestCommonAncestor(int n) : n(n), G(n), dep(n) {
        h = 1;
        while ((1 << h) <= n) h++;
        par.assign(h, vector<int>(n, -1));
    }
    void add_edge(int u, int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    void dfs(int v, int p, int d) {
        par[0][v] = p;
        dep[v] = d;
        for (int u : G[v]) {
            if (u != p) dfs(u, v, d + 1);
        }
    }
    void build(int r = 0) {
        dfs(r, -1, 0);
        for (int k = 0; k < h - 1; k++) {
            for (int v = 0; v < n; v++) {
                if (par[k][v] >= 0) {
                    par[k + 1][v] = par[k][par[k][v]];
                }
            }
        }
    }
    int lca(int u, int v) {
        if (dep[u] > dep[v]) swap(u, v);
        for (int k = 0; k < h; k++) {
            if ((dep[v] - dep[u]) & 1 << k) {
                v = par[k][v];
            }
        }
        if (u == v) return u;
        for (int k = h - 1; k >= 0; --k) {
            if (par[k][u] != par[k][v]) {
                u = par[k][u];
                v = par[k][v];
            }
        }
        return par[0][u];
    }
    int distance(int u, int v) { return dep[u] + dep[v] - dep[lca(u, v)] * 2; }
};

/**
 * @brief Dijkstra
 * @docs docs/graph/Dijkstra.md
 */
template <typename T> struct Dijkstra {
    struct edge {
        int to;
        T cost;
        edge(int to, T cost) : to(to), cost(cost) {}
        bool operator<(const edge& e) const { return cost > e.cost; }
    };
    vector<vector<edge>> G;
    vector<T> dp;
    vector<int> pre;
    Dijkstra(int n) : G(n), dp(n), pre(n) {}
    void add_edge(int u, int v, T c) { G[u].emplace_back(v, c); }
    vector<T> build(int s) {
        int n = G.size();
        fill(dp.begin(), dp.end(), numeric_limits<T>::max());
        fill(pre.begin(), pre.end(), -1);
        priority_queue<edge> pq;
        dp[s] = 0;
        pq.emplace(s, dp[s]);
        while (!pq.empty()) {
            auto p = pq.top();
            pq.pop();
            int v = p.to;
            if (dp[v] < p.cost) continue;
            for (auto e : G[v]) {
                if (dp[v] + e.cost < dp[e.to]) {
                    dp[e.to] = dp[v] + e.cost;
                    pre[e.to] = v;
                    pq.emplace(e.to, dp[e.to]);
                }
            }
        }
        return dp;
    }
    vector<int> restore(int t) {
        vector<int> res;
        if (pre[t] < 0) return res;
        while (~t) {
            res.emplace_back(t);
            t = pre[t];
        }
        reverse(res.begin(), res.end());
        return res;
    }
    T operator[](int to) { return dp[to]; }
};

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;

    LowestCommonAncestor LCA(N);
    vector<vector<pair<int, int>>> G(N);
    Dijkstra<ll> D(N + K);
    vector<int> P(K);
    for (int i = 0; i < N - 1; i++) {
        int A, B, C;
        cin >> A >> B >> C;
        A--, B--;
        G[A].emplace_back(B, C);
        G[B].emplace_back(A, C);
        LCA.add_edge(A, B);
        D.add_edge(A, B, C);
        D.add_edge(B, A, C);
    }
    for (int i = 0; i < K; i++) {
        int M;
        cin >> M >> P[i];
        for (; M--;) {
            int X;
            cin >> X;
            X--;
            D.add_edge(X, N + i, 0);
            D.add_edge(N + i, X, P[i]);
        }
    }

    LCA.build();
    vector<ll> d(N, 0);
    auto dfs = [&](auto self, int v, int p) -> void {
        if (p < 0) d[v] = 0;
        for (auto& e : G[v]) {
            int u = e.first;
            if (u == p) continue;
            d[u] = d[v] + e.second;
            self(self, u, v);
        }
    };
    dfs(dfs, 0, -1);

    vector<vector<ll>> dp;
    for (int i = 0; i < K; i++) dp.emplace_back(D.build(N + i));

    auto dist = [&](int u, int v) {
        int p = LCA.lca(u, v);
        return d[u] + d[v] - d[p] * 2;
    };

    auto query = [&](int u, int v) {
        ll res = dist(u, v);
        for (int i = 0; i < K; i++) chmin(res, dp[i][u] + dp[i][v] - P[i]);
        return res;
    };

    int Q;
    cin >> Q;
    for (; Q--;) {
        int U, V;
        cin >> U >> V;
        cout << query(--U, --V) << '\n';
    }
    return 0;
}
0