CF 375C Circling Round Treasures [DP(spfa) 状压 射线法]
题意:
在一个$n*m$的地图上,有一些障碍,还有a个宝箱和b个炸弹。你从(sx,sy)出发,走四连通的格子。你需要走一条闭合的路径,可以自交,且围出来的复杂多边形内不能包含任何炸弹。
你围出来的复杂多边形中包含的宝箱的价值和减去步数就是你的收益。
求最大收益。$n,m \le 20,a + b \le 8$
太坑了课件上的题意有问题一开始没说步数...
后来改上然后$inq[][][]$最后没从$2$改成$S$然后我就看着玄学的$inq$好长时间
和上题一样,只不过从一个点变成多个点,状压一下就好了、
更新答案的时候枚举状态计算是否有炸弹和价值和
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=,S=<<,M=N*N*S+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,g[N][N],sx,sy,val[N];
struct Object{
int x,y,id;
Object(int a=,int b=,int c=):x(a),y(b),id(c){}
bool operator <(const Object &r)const{return id<r.id;}
}a[N];
int p,t;
int dx[]={,-,, },
dy[]={, ,,-};
char s[N];
int d[N][N][S];
struct Grid{
int x,y,s;
Grid(int a=,int b=,int c=):x(a),y(b),s(c){}
}q[M];
int head,tail,inq[N][N][S];
inline void lop(int &x){if(x==M) x=;}
inline bool isInter(int a,int b,int x1,int y1,int x2,int y2){//1-->2 isInter (a,b)--right-->
if(x1<a&&x2==a&&y2>b) return ;
if(x2<a&&x1==a&&y1>b) return ;
return ;
}
int HandleInter(Grid u,Grid t){
int s=u.s;
for(int i=;i<;i++)
s^= isInter(a[i].x , a[i].y , u.x , u.y , t.x , t.y)<<i;
return s;
}
void spfa(){
d[sx][sy][]=;
head=tail=;
q[tail++]=Grid(sx,sy,);inq[sx][sy][]=;
while(head!=tail){
Grid u=q[head++];lop(head);
int x=u.x,y=u.y,s=u.s;//printf("\nnow %d %d %d\n",x,y,p);
inq[x][y][s]=;
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx<||nx>m||ny<||ny>n||g[nx][ny]) continue; Grid t(nx,ny);
int ns=t.s=HandleInter(u,t);//printf("lok %d %d %d\n",nx,ny,np);
if(d[nx][ny][ns]>d[x][y][s]+){//srintf("new %d %d %d\n",nx,ny,ns);
d[nx][ny][ns]=d[x][y][s]+;
if(!inq[nx][ny][ns])
q[tail++]=t,lop(tail),inq[nx][ny][ns]=;
}
}
}
int ans=;
for(int i=;i<S;i++) if(d[sx][sy][i]<INF){
int _=,bomb=;
for(int j=;j<;j++) if(i&(<<j)){
if(a[j].id==) {bomb=;continue;}
else _+=val[j];
}
if(bomb) continue;
ans=max(ans,_-d[sx][sy][i]);
}
printf("%d",ans);
}
int main(){
//freopen("in","r",stdin);
m=read();n=read();
memset(d,,sizeof(d));
for(int i=;i<=m;i++){
scanf("%s",s+);
for(int j=;j<=n;j++){
g[i][j]=(s[j]!='.');
if(s[j]=='S') g[i][j]=,sx=i,sy=j;
else if(s[j]=='B') a[p++]=Object(i,j,);
else if(s[j]>=''&&s[j]<='') a[p++]=Object(i,j,s[j]-''-),t++;
}
}
sort(a,a+p);
for(int i=;i<t;i++) val[i]=read();
spfa();
}
CF 375C Circling Round Treasures [DP(spfa) 状压 射线法]的更多相关文章
- Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)
题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ...
- Codeforces 375C Circling Round Treasures - 最短路 - 射线法 - 位运算
You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure wit ...
- Circling Round Treasures CodeForces - 375C
C. Circling Round Treasures time limit per test 1 second memory limit per test 256 megabytes input s ...
- HDU3247 Resource Archiver (AC自动机+spfa+状压DP)
Great! Your new software is almost finished! The only thing left to do is archiving all your n resou ...
- 树形DP和状压DP和背包DP
树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...
- dp乱写1:状态压缩dp(状压dp)炮兵阵地
https://www.luogu.org/problem/show?pid=2704 题意: 炮兵在地图上的摆放位子只能在平地('P') 炮兵可以攻击上下左右各两格的格子: 而高原('H')上炮兵能 ...
- poj2411 Mondriaan's Dream (轮廓线dp、状压dp)
Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17203 Accepted: 991 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- 【BZOJ-1097】旅游景点atr SPFA + 状压DP
1097: [POI2007]旅游景点atr Time Limit: 30 Sec Memory Limit: 357 MBSubmit: 1531 Solved: 352[Submit][Sta ...
随机推荐
- SecureCRT连接虚拟机中的Linux系统(Ubuntu)_Linux教程
有道云笔记链接地址: https://note.youdao.com/share/?id=826781e7ca1fd1223f6a43f4dc2c9b5d&type=note#/
- Spark算子--first、count、reduce、collect、lookup
转载请标明出处http://www.cnblogs.com/haozhengfei/p/4b8582c8dde1529abb11e4ccc8296171.html first.count.reduce ...
- java中static关键字的继承问题
结论:java中静态属性和静态方法可以被继承,但是没有被重写(overwrite)而是被隐藏. 原因: 1). 静态方法和属性是属于类的,调用的时候直接通过类名.方法名完成对,不需要继承机制及可以调用 ...
- PHP操作MySQL对表增加一列(一个字段)
2014-03-19 16:59 1471人阅读 评论(0) 收藏 举报 分类: MySQL(12) 对于已经建立好的数据库,在一个已经有字段的表内新加字段可用以下方法: mysql_query(&q ...
- zoom:1是什么意思
当一个容器内元素都浮动后,它将高度将不会随着内部元素高度的增加而增加,所以造成内容元素的显示超出了容器. overflow:auto;是让高度自适应, zoom:1;是为了兼容IE6,也可以用heig ...
- dedecms====phpcms 区别==[工作]
{template "content","header"}{dede:include filename="head.htm"/} ----- ...
- Apache 403 错误解决方法-让别人可以访问你的服务器
参考网址:http://www.cnblogs.com/mrlaker/archive/2013/04/29/3050888.html http://www.jb51.net/article/6119 ...
- ABB中断设定
简介: 中断是程序定义事件,通过中断编号识别.中断发生在中断条件为真时.中断不同于其他错误,前者与特定消息号位置无直接关系(不同步).中断会导致正常程序执行过程暂停,跳过控制,进入软中断程序. 即使机 ...
- linux 如何降低入向软中断占比
最近遇到一个问题,当tcp收包的时候,我们的服务器的入向软中断比例很高. 我们知道,napi模式,可以降低收包入向软中断占比,那么,针对napi模式,能不能优化?本文针对2.6.32-358内核进行分 ...
- Hyperledger Fabric CouchDB as the State Database
使用CouchDB作为状态数据库 状态数据库选项 状态数据库包括LevelDB和CouchDB.LevelDB是嵌入在peer进程中的默认键/值状态数据库,CouchDB是一个可选的外部状态数据库.与 ...