結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー hitonanodehitonanode
提出日時 2024-02-17 10:53:32
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 775 ms / 2,500 ms
コード長 1,239 bytes
コンパイル時間 1,215 ms
コンパイル使用メモリ 107,424 KB
実行使用メモリ 43,392 KB
最終ジャッジ日時 2024-09-28 23:35:47
合計ジャッジ時間 11,564 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

#include <atcoder/dsu>

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

    int N, M;
    cin >> N >> M;

    vector<pair<int, int>> edges(M);
    for (auto &[a, b] : edges) cin >> a >> b, --a, --b;

    vector<int> C(N);
    for (auto &c : C) cin >> c, --c;

    constexpr int D = 10;

    vector<long long> W(D);
    for (auto &w : W) cin >> w;

    vector<long long> Wsum(1 << D);
    for (int S = 0; S < (1 << D); ++S) {
        for (int i = 0; i < D; ++i) {
            if (S & (1 << i)) Wsum.at(S) += W.at(i);
        }
    }

    vector<atcoder::dsu> dsus(1 << D);
    for (int S = 0; S < (1 << D); ++S) {
        dsus.at(S) = atcoder::dsu(N);
        for (auto [a, b] : edges) {
            if ((S & 1 << C.at(a)) and (S & 1 << C.at(b))) dsus.at(S).merge(a, b);
        }
    }

    int Q;
    cin >> Q;

    constexpr long long inf = 1e18;

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

        long long best = inf;
        for (int S = 0; S < (1 << D); ++S) {
            if (dsus.at(S).same(u, v)) best = min(best, Wsum.at(S));
        }

        if (best == inf) best = -1;

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