結果
| 問題 |
No.2630 Colorful Vertices and Cheapest Paths
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-02-18 17:45:27 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,376 ms / 2,500 ms |
| コード長 | 2,296 bytes |
| コンパイル時間 | 1,253 ms |
| コンパイル使用メモリ | 108,172 KB |
| 実行使用メモリ | 44,032 KB |
| 最終ジャッジ日時 | 2024-09-29 00:45:06 |
| 合計ジャッジ時間 | 20,371 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class UnionFind {
private:
int n;
vector<int> par;
int group_;
public:
UnionFind(int n) : n(n), par(n, -1), group_(n) {}
int find(int x) {
if (par[x] < 0)
return x;
vector<int> lst;
while (par[x] >= 0) {
lst.push_back(x);
x = par[x];
}
for (int y : lst) {
par[y] = x;
}
return x;
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
group_--;
return true;
}
int size(int x) {
return -par[find(x)];
}
bool same(int x, int y) {
return find(x) == find(y);
}
int group() {
return group_;
}
};
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> ab(m, vector<int>(2));
for (int i = 0; i < m; ++i) {
cin >> ab[i][0] >> ab[i][1];
}
vector<long long> C(n), W(10);
for (int i = 0; i < n; ++i) {
cin >> C[i];
C[i]--;
}
for (int i = 0; i < 10; ++i) {
cin >> W[i];
}
int t = 10;
vector<UnionFind> UF(1 << t, UnionFind(n));
vector<long long> cost(1 << t, 0);
for (int bit = 0; bit < (1 << t); ++bit) {
for (int i = 0; i < t; ++i) {
if (bit >> i & 1) {
cost[bit] += W[i];
}
}
}
for (auto& edge : ab) {
edge[0]--;
edge[1]--;
int bit = (1 << C[edge[0]]) | (1 << C[edge[1]]);
for (int S = 0; S < (1 << t); ++S) {
if ((S & bit) == bit) {
UF[S].unite(edge[0], edge[1]);
}
}
}
int Q;
cin >> Q;
for (int q = 0; q < Q; ++q) {
int x, y;
cin >> x >> y;
x--;
y--;
long long ans = cost[(1 << t) - 1] + 1;
for (int S = 0; S < (1 << t); ++S) {
if (UF[S].same(x, y)) {
ans = min(ans, cost[S]);
}
}
if (ans == cost[(1 << t) - 1] + 1) {
ans = -1;
}
cout << ans << endl;
}
return 0;
}