BZOJ4992 [Usaco2017 Feb]Why Did the Cow Cross the Road 最短路 SPFA
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ4992
题意概括
在一幅n*n的地图上,Amber从左上角走到右下角,每走一步需要花费时间t,每走完3步时,还要加上到达的那个格子的值。这里的3步不包括起动的那个格子。如果刚好3步到达右下角,则右下角格子的值也要算进花费中,否则不用计算进去。求最小花费。n<=100
题解
最短路写一写就可以了,居然不卡spfa!
有一个点要注意:每一个点要建16条边,我就是因为少建了4条边,少了100分……
是这样的:每走3步建一条边,与该点距离为3的点,每个一条边,共12条;与该点距离为1的点,也要建边,这个就是我万万没想到的WA掉了。这四条边的结果,还不如打暴力!当然别忘了建连向终点的5条特殊的边。
代码
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=100+5,M=N*N*16;
const int dx[16]={-3, 0, 3, 0,-2,-2, 2, 2,-1,-1, 1, 1, 1,-1, 0, 0};
const int dy[16]={ 0,-3, 0, 3, 1,-1, 1,-1, 2,-2, 2,-2, 0, 0, 1,-1};
int n;
LL a[N][N],t;
struct Edge{
int cnt,y[M],nxt[M],fst[N*N];
LL z[M];
void set(){
cnt=0;
memset(y,0,sizeof y);
memset(z,0,sizeof z);
memset(nxt,0,sizeof nxt);
memset(fst,0,sizeof fst);
}
void add(int a,int b,LL c){
cnt++;
y[cnt]=b,z[cnt]=c;
nxt[cnt]=fst[a],fst[a]=cnt;
}
}e;
bool check(int x,int y){
return 1<=x&&x<=n&&1<=y&&y<=n;
}
int hash(int x,int y){
return (x-1)*n+y-1;
}
void adden(int x,int y){
if (!check(x,y))
return;
e.add(hash(x,y),hash(n,n),t*(abs(n-x)+abs(n-y)));
}
LL dis[N*N];
bool f[N*N];
int q[N*N],qmod,head,tail;
void spfa(){
for (int i=0;i<n*n;i++)
dis[i]=1LL<<55;
memset(f,0,sizeof f);
qmod=n*n+1;
head=0,tail=0;
q[tail=(tail+1)%qmod]=0;
dis[0]=0;
f[0]=1;
while (head!=tail){
int x=q[head=(head+1)%qmod];
f[x]=0;
for (int i=e.fst[x];i;i=e.nxt[i]){
int y=e.y[i];
LL z=e.z[i];
if (dis[y]>dis[x]+z){
dis[y]=dis[x]+z;
if (!f[y]){
f[y]=1;
q[tail=(tail+1)%qmod]=y;
}
}
}
}
}
int main(){
scanf("%d%lld",&n,&t);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
scanf("%lld",&a[i][j]);
e.set();
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
int x=hash(i,j);
for (int k=0;k<16;k++){
int i_=i+dx[k],j_=j+dy[k];
if (check(i_,j_))
e.add(x,hash(i_,j_),t*3LL+a[i_][j_]);
}
}
adden(n-2,n),adden(n-1,n-1),adden(n,n-2),adden(n-1,n),adden(n,n-1);
spfa();
printf("%lld",dis[hash(n,n)]);
return 0;
}
BZOJ4992 [Usaco2017 Feb]Why Did the Cow Cross the Road 最短路 SPFA的更多相关文章
- [BZOJ4992] [Usaco2017 Feb]Why Did the Cow Cross the Road(spfa)
传送门 把每个点和曼哈顿距离距离它3步或1步的点连一条边,边权为3 * t + a[x][y] 因为,走3步,有可能是3步,也有可能是1步(其中一步拐了回来) 最后,把终点和曼哈顿距离距离它1步和2布 ...
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp
题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...
- 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...
- [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp
4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec Memory Limit: 128 MBSubmi ...
- [BZOJ4989][Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组维护逆序对
4989: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec Memory Limit: 256 MBSubmit: ...
- [bzoj4994][Usaco2017 Feb]Why Did the Cow Cross the Road III_树状数组
Why Did the Cow Cross the Road III bzoj-4994 Usaco-2017 Feb 题目大意:给定一个长度为$2n$的序列,$1$~$n$个出现过两次,$i$第一次 ...
- BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...
- BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...
- BZOJ4989 [Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组 逆序对
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4989 题意概括 一条马路的两边分别对应的序列A.B,长度为n,两序列为1到n的全排列.当Ai=Bj ...
随机推荐
- 22. SpringBoot 集成 Mybatis
1. 引入Mybatis的maven 依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> < ...
- Mac下IntelliJ IDEA快捷键大全
Mac键盘符号和修饰键说明⌘ Command⇧ Shift⌥ Option⌃ Control↩︎ Return/Enter⌫ Delete⌦ 向前删除键(Fn+Delete)↑ 上箭头↓ 下箭头← 左 ...
- 使用 CSS3 的 box-sizing 属性设置元素大小包含 border 与 padding
Ø 默认情况下,内部元素(如:input)的宽度或高度,是不会包含元素的边框和内边距的,这时就需要使用 box-sizing 属性设置该元素. Ø box-sizing 是 CSS3 的属性,可以 ...
- 破解WEP工具wesside-ng
1.关于wesside-ng wesside-ng是一款自动化的WEP破解工具,该工具采用了多种WEP加密破解技术.它首先会自动明确目标无线网络,然后尝试与之相关联,在获得PRGA(伪随机生成算法)异 ...
- tarjan,树剖,倍增求lca
1.tarjan求lca 思想: void tarjan(int u,int f){ for(int i=---){//枚举边 if(v==f) continue; dfs(v); //继续搜 uni ...
- mysql 原理 ~ binlog
一 简介:我们会持续对binlog进行分析,但是不深入代码二 版本 5.6 格式 GTID和传统格式 传统格式 一 binlog针对具体事务注意点-1 1 u ...
- Linux文件系统3--打开文件
1.前言 本文所述关于文件管理的系列文章主要是对陈莉君老师所讲述的文件系统管理知识讲座的整理. Linux可以支持不同的文件系统,它源于unix文件系统,也是unix文件系统的一大特色. 本文将以不同 ...
- setInterval的用法
function show1(){ console.log("每隔1秒显示一次");}function show2(str){ console.log(str);}se ...
- Qt5.8 在windows下mingw静态编译
官方对编译一些条件介绍:https://doc.qt.io/qt-5/windows-requirements.html 在默认情况下,用QtCreator编译程序时,使用的是动态编译.编译好的程序在 ...
- DOS命令大全(转)
dos命令大全 CMD是command的缩写,是windows环境下的虚拟DOS窗口,提供有DOS命令,功能强大,如果你以前学习过DOS操作,那就小儿科了.是基于Windows的命令行窗口,在开始-- ...