版本比较
题解:P13714 淘汰(Hard ver.)
正文
增加 33 行删除 33 行
27 行未改动2829303132323334353637383839404142434444454647484950505152535415 行未改动7071727374747576777879798081828384858687888889909192939495959697989910010110210210310410510610710810911011111111211311311411511611711811912012112212312312412512612712712812913013113213313413513613613713813914014114214314314414514614714814915015015115215315415515615715815915916016116116216316416516616716816917017117117217317417517517617717817918018018118218318415 行未改动20020120220320420420520620720820920921021121221321421521521621721821921922022122222322422422522622722822923023123123223323423423523623723823924024124124224324424524624724724824925025125225325425425525625725825926026126126226326426526626626726826927041 行未改动27 行未改动
int main() { ios::sync_with_stdio(false); cin.tie(0);
int T; cin >> T; while (T--) { int n, k, x, y; cin >> n >> k >> x >> y;
vector<int> a(n), b(n), c(n), d(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 0; i < n; i++) cin >> d[i];
// 步骤1: 初步可行性检查 bool possible = true; for (int i = 0; i < k; i++) { int x_bit = (x >> i) & 1; int y_bit = (y >> i) & 1;
if (x_bit > y_bit) { // 需要有a_i在该位为0才能将1变为0 bool has_valid = false; for (int ai : a) {15 行未改动 if (!has_valid) possible = false; } if (!possible) break; }
if (!possible) { cout << -1 << "\n"; continue; }
// 步骤2: 优化AND操作集 unordered_map<int, int> and_map; for (int i = 0; i < n; i++) { // 保留每个a值的最小代价 if (and_map.find(a[i]) == and_map.end() || c[i] < and_map[a[i]]) { and_map[a[i]] = c[i]; } }
// 按代价排序 vector<pair<int, int>> sorted_and; // (代价, a值) for (auto& p : and_map) { sorted_and.emplace_back(p.second, p.first); } sort(sorted_and.begin(), sorted_and.end());
// 筛选非冗余操作 vector<pair<int, int>> keep_and; // (代价, a值) for (auto& p : sorted_and) { int cost = p.first; int a_val = p.second; bool redundant = false;
// 检查是否被已保留的操作覆盖 for (auto& q : keep_and) { int ap = q.second; if ((ap & a_val) == ap) { redundant = true; break; } }
if (redundant) continue;
// 移除被当前操作覆盖的已保留操作 vector<pair<int, int>> new_keep; for (auto& q : keep_and) { int ad = q.second; int cd = q.first; if ((a_val & ad) != a_val) { new_keep.emplace_back(cd, ad); } }
new_keep.emplace_back(cost, a_val); keep_and = move(new_keep); }
// 步骤3: 优化OR操作集 unordered_map<int, int> or_map; for (int i = 0; i < n; i++) { // 保留每个b值的最小代价 if (or_map.find(b[i]) == or_map.end() || d[i] < or_map[b[i]]) { or_map[b[i]] = d[i]; } }
// 按代价排序 vector<pair<int, int>> sorted_or; // (代价, b值) for (auto& p : or_map) { sorted_or.emplace_back(p.second, p.first); } sort(sorted_or.begin(), sorted_or.end());
// 筛选非冗余操作 vector<pair<int, int>> keep_or; // (代价, b值) for (auto& p : sorted_or) { int cost = p.first; int b_val = p.second; bool redundant = false;
// 检查是否被已保留的操作覆盖 for (auto& q : keep_or) { int bp = q.second; if ((bp | b_val) == bp) { redundant = true; break; } }
if (redundant) continue;
// 移除被当前操作覆盖的已保留操作 vector<pair<int, int>> new_keep; for (auto& q : keep_or) { int bd = q.second; int dd = q.first; if ((b_val | bd) != b_val) { new_keep.emplace_back(dd, bd); } }
new_keep.emplace_back(cost, b_val); keep_or = move(new_keep); }
// 步骤4: 二次可行性检查(确保优化后的操作仍满足需求) for (int i = 0; i < k; i++) { int x_bit = (x >> i) & 1; int y_bit = (y >> i) & 1;
if (x_bit > y_bit) { bool has_valid = false; for (auto& p : keep_and) { int a_val = p.second;15 行未改动 if (!has_valid) possible = false; } if (!possible) break; }
if (!possible) { cout << -1 << "\n"; continue; }
// 步骤5: Dijkstra算法求解最短路径 int size = 1 << k; // 总状态数: 2^k ll nor = INF; // 题目要求使用nor作为变量名 vector<ll> dist(size, nor); dist[x] = 0;
// 优先队列: (代价, 状态) priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; pq.emplace(0, x);
bool found = false; while (!pq.empty()) { auto [current_cost, s] = pq.top(); pq.pop();
// 到达目标状态 if (s == y) { cout << current_cost << "\n"; found = true; break; }
// 剪枝: 当前代价已大于已知最小代价 if (current_cost > dist[s]) continue;
// 处理AND操作 for (auto& p : keep_and) { int cost = p.first; int a_val = p.second; int t = s & a_val; ll new_cost = current_cost + cost;
if (new_cost < dist[t]) { dist[t] = new_cost; pq.emplace(new_cost, t); } }
// 处理OR操作 for (auto& p : keep_or) { int cost = p.first; int b_val = p.second; int t = s | b_val; ll new_cost = current_cost + cost;
if (new_cost < dist[t]) { dist[t] = new_cost; pq.emplace(new_cost, t); } } }
if (!found) { cout << -1 << "\n"; } }
return 0;}
```41 行未改动