結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー haihamabossu
提出日時 2024-02-16 22:48:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 281 ms / 2,500 ms
コード長 1,176 bytes
コンパイル時間 2,372 ms
コンパイル使用メモリ 205,124 KB
最終ジャッジ日時 2025-02-19 14:32:39
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/dsu>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const ll INF = 1e18;
void solve() {
  ll n, m;
  cin >> n >> m;
  vector<pair<ll, ll>> ab(m);
  rep(i, m) {
    ll a, b;
    cin >> a >> b, a--, b--;
    ab[i] = {a, b};
  }
  vector<ll> c(n), w(10);
  rep(i, n) cin >> c[i], c[i]--;
  rep(i, 10) cin >> w[i];
  ll q;
  cin >> q;
  vector<pair<ll, ll>> qs(q);
  rep(qi, q) {
    ll a, b;
    cin >> a >> b, a--, b--;
    qs[qi] = {a, b};
  }
  vector<ll> ans(q, INF);
  rep(S, 1 << 10) {
    ll sumw = 0;
    atcoder::dsu d(n);
    rep(i, 10) if ((S >> i) & 1) sumw += w[i];
    for (const auto &[a, b] : ab) {
      if (!d.same(a, b))
        if ((S >> c[a]) & 1)
          if ((S >> c[b]) & 1)
            d.merge(a, b);
    }
    rep(qi, q) {
      const auto &[u, v] = qs[qi];
      if (d.same(u, v)) {
        ans[qi] = min(ans[qi], sumw);
      }
    }
  }
  rep(qi, q) cout << (ans[qi] == INF ? -1 : ans[qi]) << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0