版本比较
题解:P13714 淘汰(Hard ver.)
正文
增加 33 行删除 33 行
27 行未改动28293031323334353637383940414243444546474849505152535415 行未改动70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318415 行未改动20020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927041 行未改动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变为0bool 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^kll 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 行未改动27 行未改动28293031323334353637383940414243444546474849505152535415 行未改动70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318415 行未改动20020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927041 行未改动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变为0bool 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^kll 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 行未改动