結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー haihamabossuhaihamabossu
提出日時 2024-02-16 22:48:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 341 ms / 2,500 ms
コード長 1,176 bytes
コンパイル時間 2,410 ms
コンパイル使用メモリ 212,436 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-02-16 22:49:14
合計ジャッジ時間 8,326 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
6,548 KB
testcase_01 AC 277 ms
6,548 KB
testcase_02 AC 270 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 3 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 127 ms
6,548 KB
testcase_07 AC 233 ms
6,548 KB
testcase_08 AC 220 ms
6,548 KB
testcase_09 AC 239 ms
6,548 KB
testcase_10 AC 314 ms
6,548 KB
testcase_11 AC 314 ms
6,548 KB
testcase_12 AC 313 ms
6,548 KB
testcase_13 AC 341 ms
6,548 KB
testcase_14 AC 312 ms
6,548 KB
testcase_15 AC 295 ms
6,548 KB
testcase_16 AC 293 ms
6,548 KB
testcase_17 AC 321 ms
6,548 KB
testcase_18 AC 134 ms
6,548 KB
testcase_19 AC 194 ms
6,548 KB
testcase_20 AC 173 ms
6,548 KB
testcase_21 AC 226 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

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