2022杭电多校#1
B Dragon slayer
题意:在一个n*m的地图中已知起点和终点,还有若干堵墙,拆掉一堵墙需要一点体力,求从起点到终点最少消耗的体力。
分析:数据只有15,直接暴力建图;用二进制表示某一堵墙是否存在,若存在加入图;最小体力就是求最多数量的墙,在每一次建图之前先计算需要建造墙的数量,若cnt小于我们之前通过时建造的墙的数量,那么该次搜索无效(没有这一步会TLE);最后用bfs判断是否通过,更新res,最后输出k-res;
#include<iostream>
#include<cstring>
#include<queue>
#include<map>
#define debug() system("pause")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int mod = 1e9+7, N = 50;
struct Wall{
int x1, y1, x2, y2;
}walls[N];
int g[N][N], bk[N][N];
int xs, ys, xt, yt;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k;
void deb(){
for (int i = 0; i <= n; i++){
for (int j = 0; j <= m; j++){
cout << bk[i][j];
}
cout << endl;
}
cout << endl;
}
bool bfs(){
//deb();
queue<PII> q;
q.push({xs, ys});
PII end = {xt, yt};
bk[xs][ys] = 1;
while (q.size()){
PII t = q.front();q.pop();
if (t == end){
return true;
}
int xxt = t.first, yyt = t.second;
for (int i = 0; i < 4; i++){
int x = xxt+dx[i], y = yyt+dy[i];
if (x > 0 && x < n && y > 0 && y < m &&bk[x][y] == 0){
q.push({x, y});
bk[x][y] = 1;
}
}
}
return false;
}
void solve(){
cin >> n >> m >> k;
n*=2;m*=2;
cin >> xs >> ys >> xt >> yt;
(xs *= 2) += 1;
(ys *= 2) += 1;
(xt *= 2) += 1;
(yt *= 2) += 1;
for (int i = 0; i < k; i++){
int a, b, c, d;
cin >> a >> b >> c >> d;
a*=2,b*=2,c*=2,d*=2;
walls[i] = {a, b, c, d};
}
int res = 0;
for (int i = 0; i < 1<<k; i++){
int cnt = 0;
memcpy(bk, g, sizeof bk);
for (int j = 0; j < k; j++)
if (((i>>j)&1))
cnt++;
if (cnt <= res) continue;
for (int j = 0; j < k; j++){
if (((i>>j)&1)){
int a = walls[j].x1, b = walls[j].y1, c = walls[j].x2, d = walls[j].y2;
if (a == c){
for (int j = (b < d? b: d); j <= (b > d? b: d); j++){
bk[a][j] = 1;
}
}
else {
for (int j = (a < c? a: c); j <= (a > c? a: c); j++){
bk[j][b] = 1;
}
}
}
}
if (bfs()) res = max(res, cnt);
}
cout << k-res << endl;
}
int main(){
int t;
cin >> t;
while (t--){
solve();
}
debug();
return 0;
}
C Backpack
题意:给定一个大小背包,背个物品有体积和价值,求如何在恰好塞满背包的前提下让所有背包里的物品价值总异或和最大。
分析: 背包问题,2^10,正常写暴力背包的话f(i, j, k), 表示前i个物品中,当前容量为j且物品的最大异或和为k的是否存在,这样的话时间复杂度是n^3会超时。 我们发现每一个值对应的都是0或者1,可以用bitset。状态转移方程为f[j] |= f[j - v[i]] , f[j - v[i]]也就是f[j]左移v[i]的值,我们在更新前用一个g数组储存每一个f左移v[i]的值, 然后更新。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1030;
bitset<N> f[N], g[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
f[0][0] = 1;
for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = 0; j < 1024; j++) {
g[j] = f[j];
g[j] <<= v;
}
for (int j = 0; j < 1024; j++) {
f[j] |= g[j ^ w];
}
}
int ans = -1;
for (int j = 0; j < 1024; j++) {
if (f[j][m]) {
ans = j;
}
}
cout << ans << endl;
for (int i = 0; i <= 1024; i++)
f[i].reset(), g[i].reset();
}
return 0;
}
I Laser
题意:棋盘中有n个点,请问是否存在一个点,他朝8个方向射出的光线能否包含所有点(米字形)。
分析:一次枚举,米字的四个方向。然后我们判断一下是否能包含所有点。直接从第一个点开始,寻找与他不同位置的点最后找到三个中心判断。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
using namespace std;
#define int long long
const int N = 5e5 + 10, M = N * 2, INF = 1e18;
int n, m, s, k;
int h[N], ne[M], e[M], w[M], p[M], idx;
struct Node
{
int a, d, p;
bool operator < (const Node &t) const
{
return d > t.d;
}
};
bool st[N][2];
int dist[N][2];
int gg[N]; // 标记
void add(int a, int b, int c, int d) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, p[idx] = d, ne[idx] = h[a], h[a] = idx ++ ;
}
void bfs()
{
set<int> S;
for(int i = 1 ; i <= n ; i ++ )
{
if(i != s) S.insert(i);
st[i][0] = st[i][1] = false;
dist[i][0] = dist[i][1] = INF;
}
dist[s][0] = 0;
priority_queue<Node> q;
q.push({s, 0, 0});
int cnt = 0;
while(q.size())
{
auto t = q.top();
int ty = t.p, ver = t.a;
q.pop();
cnt ++ ;
if(!ty) S.erase(ver);
else
{
for(int i = h[ver] ; ~i ; i = ne[i])
{
int j = e[i];
gg[j] = cnt; // 这个点是邻边
}
vector<int> temp;
for(auto it : S)
{
if(gg[it] != cnt) // 没有被遍历的非相邻的点
{
temp.push_back(it);
dist[it][0] = dist[ver][ty];
q.push({it, dist[it][0], 0});
}
}
for(auto it : temp) S.erase(it); // 被更新的点被遍历过 所以直接删除
}
int y = 0;
if(ty) y -= k;
if(st[ver][ty]) continue;
st[ver][ty] = true;
for(int i = h[ver] ; ~i ; i = ne[i])
{
int j = e[i];
if(dist[j][p[i]] > dist[ver][ty] + w[i] + y)
{
dist[j][p[i]] = dist[ver][ty] + w[i] + y;
q.push({j, dist[j][p[i]], p[i]});
}
}
}
}
void solve()
{
cin >> n >> m >> s >> k;
for(int i = 0 ; i <= n ; i ++ ) h[i] = -1, gg[i] = 0;
idx = 0;
for(int i = 1 ; i <= m ; i ++ )
{
int a, b, c, d;
cin >> a >> b >> c >> d;
add(a, b, c, d);
}
bfs();
for(int i = 1 ; i <= n ; i ++ )
if(min(dist[i][0], dist[i][1]) == INF) cout << -1 << " ";
else cout << min(dist[i][0], dist[i][1]) << " ";
cout << endl;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0);
int T = 1;
cin >> T;
while(T -- ) solve();
return 0;
}