結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー 👑 emthrmemthrm
提出日時 2024-02-16 21:39:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 790 ms / 2,500 ms
コード長 2,128 bytes
コンパイル時間 3,262 ms
コンパイル使用メモリ 253,308 KB
実行使用メモリ 43,520 KB
最終ジャッジ日時 2024-02-16 21:40:01
合計ジャッジ時間 13,134 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
12,672 KB
testcase_01 AC 475 ms
23,552 KB
testcase_02 AC 653 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 87 ms
8,320 KB
testcase_07 AC 570 ms
43,008 KB
testcase_08 AC 528 ms
38,528 KB
testcase_09 AC 592 ms
43,520 KB
testcase_10 AC 790 ms
43,520 KB
testcase_11 AC 763 ms
43,520 KB
testcase_12 AC 786 ms
43,520 KB
testcase_13 AC 766 ms
43,520 KB
testcase_14 AC 786 ms
43,520 KB
testcase_15 AC 145 ms
6,676 KB
testcase_16 AC 146 ms
6,676 KB
testcase_17 AC 148 ms
6,676 KB
testcase_18 AC 277 ms
14,848 KB
testcase_19 AC 496 ms
34,176 KB
testcase_20 AC 418 ms
28,672 KB
testcase_21 AC 555 ms
43,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct UnionFind {
  explicit UnionFind(const int n) : data(n, -1) {}

  int root(const int ver) {
    return data[ver] < 0 ? ver : data[ver] = root(data[ver]);
  }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool is_same(const int u, const int v) { return root(u) == root(v); }

  int size(const int ver) { return -data[root(ver)]; }

 private:
  std::vector<int> data;
};

int main() {
  constexpr int C = 10;
  int n, m; cin >> n >> m;
  vector<int> a(m), b(m); REP(i, m) cin >> a[i] >> b[i], --a[i], --b[i];
  vector<int> c(n);
  for (int& c_i : c) cin >> c_i, --c_i;
  array<int, C> w{};
  for (int& w_i : w) cin >> w_i;
  vector uf(1 << C, UnionFind(n));
  FOR(bit, 1, 1 << C) REP(i, m) {
    if ((bit >> c[a[i]] & 1) && (bit >> c[b[i]] & 1)) uf[bit].unite(a[i], b[i]);
  }
  array<ll, 1 << C> cost{};
  FOR(bit, 1, 1 << C) REP(i, C) {
    if (bit >> i & 1) cost[bit] += w[i];
  }
  int q; cin >> q;
  while (q--) {
    int u, v; cin >> u >> v; --u; --v;
    ll ans = LINF;
    FOR(bit, 1, 1 << C) {
      if (uf[bit].is_same(u, v)) chmin(ans, cost[bit]);
    }
    cout << (ans == LINF ? -1 : ans) << '\n';
  }
  return 0;
}
0