結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー ayataka5ayataka5
提出日時 2024-02-16 23:34:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,071 ms / 2,500 ms
コード長 1,875 bytes
コンパイル時間 2,191 ms
コンパイル使用メモリ 209,580 KB
実行使用メモリ 43,520 KB
最終ジャッジ日時 2024-02-16 23:34:34
合計ジャッジ時間 14,023 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 170 ms
12,928 KB
testcase_01 AC 627 ms
23,552 KB
testcase_02 AC 873 ms
39,808 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 101 ms
8,320 KB
testcase_07 AC 617 ms
42,880 KB
testcase_08 AC 606 ms
38,528 KB
testcase_09 AC 617 ms
43,520 KB
testcase_10 AC 1,010 ms
43,520 KB
testcase_11 AC 1,071 ms
43,520 KB
testcase_12 AC 1,047 ms
43,520 KB
testcase_13 AC 1,041 ms
43,520 KB
testcase_14 AC 1,036 ms
43,520 KB
testcase_15 AC 85 ms
6,676 KB
testcase_16 AC 85 ms
6,676 KB
testcase_17 AC 87 ms
6,676 KB
testcase_18 AC 261 ms
15,104 KB
testcase_19 AC 466 ms
34,176 KB
testcase_20 AC 407 ms
28,544 KB
testcase_21 AC 498 ms
43,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int N) : data(N, -1) {}

  int find(int k) { return data[k] < 0 ? k : data[k] = find(data[k]); }

  int unite(int x, int y) {
    if ((x = find(x)) == (y = find(y))) return false;
    if (data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  // f ... merge function
  template<typename F>
  int unite(int x, int y,const F &f) {
    if ((x = find(x)) == (y = find(y))) return false;
    if (data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    f(x, y);
    return true;
  }

  int size(int k) { return -data[find(k)]; }

  int same(int x, int y) { return find(x) == find(y); }
};

int main() {
    int N, M;
    cin >> N >> M;
    vector A(M, 0), B(M, 0);
    for(int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--, B[i]--; }
    vector C(N, 0);
    for(int i = 0; i < N; i++) { cin >> C[i]; C[i]--; }
    vector W(10, 0LL);
    for(int i = 0; i < 10; i++) cin >> W[i];
    vector<UnionFind> uf(1024, UnionFind(N));
    for(int i = 0; i < M; i++) {
        for(int bit = 0; bit < 1024; bit++) {
            if((bit >> C[A[i]] & 1) && (bit >> C[B[i]] & 1)) {
                uf[bit].unite(A[i], B[i]);
            }
        }
    }
    vector cost(1024, 0LL);
    for(int bit = 0; bit < 1024; bit++) {
        for(int i = 0; i < 10; i++) {
            if(bit >> i & 1) {
                cost[bit] += W[i];
            }
        }
    }
    int Q;
    cin >> Q;
    while(Q--) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        long long ans(20000000000LL);
        for(int bit = 0; bit < 1024; bit++) {
            if(uf[bit].same(u, v)) ans = min(cost[bit], ans);
        }
        if(ans == 20000000000LL) {
            ans = -1LL;
        }
        cout << ans << endl;
    }
    return 0;
}
0