結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-02-19 11:03:14
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 686 ms / 2,500 ms
コード長 1,421 bytes
コンパイル時間 2,380 ms
コンパイル使用メモリ 211,408 KB
実行使用メモリ 43,392 KB
最終ジャッジ日時 2024-09-29 01:08:46
合計ジャッジ時間 11,013 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/dsu>
using namespace std;
using namespace atcoder;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

int main() {
    fast_io();
    int n, m;
    cin >> n >> m;
    vector<int> a(m), b(m);
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
    }
    vector<int> c(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        c[i]--;
    }
    vector<long long> w(10);
    for (int i = 0; i < 10; i++) {
        cin >> w[i];
    }
    vector<dsu> uf(1 << 10, dsu(n));
    vector<long long> cst(1 << 10);
    for (int st = 0; st < (1 << 10); st++) {
        for (int i = 0; i < 10; i++) {
            if (st & (1 << i)) {
                cst[st] += w[i];
            }
        }
        for (int i = 0; i < m; i++) {
            if ((st & (1 << c[a[i]])) && (st & (1 << c[b[i]]))) {
                uf[st].merge(a[i], b[i]);
            }
        }
    }
    int q;
    cin >> q;
    const long long INF = 9e18;
    while (q--) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        long long ans = INF;
        for (int st = 0; st < (1 << 10); st++) {
            if (uf[st].same(u, v)) {
                ans = min(ans, cst[st]);
            }
        }
        if (ans == INF) {
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
}
0