传送门

把每个点和曼哈顿距离距离它3步或1步的点连一条边,边权为3 * t + a[x][y]

因为,走3步,有可能是3步,也有可能是1步(其中一步拐了回来)

最后,把终点和曼哈顿距离距离它1步和2布的点连一条边,边权为 步数 * t

跑一边spfa即可

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1000001
#define idx(i, j) ((i - 1) * n + j) using namespace std; int n, t, cnt;
int a[101][101], d[4][N][2], tot[4];
int head[N], to[N], next[N], val[N], dis[N];
bool vis[N];
queue <int> q; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y, int z)
{
to[cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt++;
} inline void spfa()
{
int i, u, v;
memset(dis, 127, sizeof(dis));
dis[1] = 0;
q.push(1);
while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(i = head[u]; ~i; i = next[i])
{
v = to[i];
if(dis[v] > dis[u] + val[i])
{
dis[v] = dis[u] + val[i];
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
} int main()
{
int i, j, k, l, x, y;
n = read();
t = read();
for(i = 1; i <= 3; i++)
for(j = 0; j <= i; j++)
{
d[i][++tot[i]][0] = j, d[i][tot[i]][1] = i - j;
d[i][++tot[i]][0] = j, d[i][tot[i]][1] = -i + j;
d[i][++tot[i]][0] = -j, d[i][tot[i]][1] = i - j;
d[i][++tot[i]][0] = -j, d[i][tot[i]][1] = -i + j;
}
memset(head, -1, sizeof(head));
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
a[i][j] = read();
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
for(l = 1; l <= 3; l += 2)
for(k = 1; k <= tot[l]; k++)
{
x = i + d[l][k][0];
y = j + d[l][k][1];
if(1 <= x && x <= n && 1 <= y && y <= n)
add(idx(i, j), idx(x, y), 3 * t + a[x][y]);
}
for(i = 1; i <= 2; i++)
for(j = 1; j <= tot[i]; j++)
{
x = n + d[i][j][0];
y = n + d[i][j][1];
if(1 <= x && x <= n && 1 <= y && y <= n)
add(idx(x, y), n * n, i * t);
}
spfa();
printf("%d\n", dis[n * n]);
return 0;
}

  

[BZOJ4992] [Usaco2017 Feb]Why Did the Cow Cross the Road(spfa)的更多相关文章

  1. BZOJ4992 [Usaco2017 Feb]Why Did the Cow Cross the Road 最短路 SPFA

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4992 题意概括 在一幅n*n的地图上,Amber从左上角走到右下角,每走一步需要花费时间t,每走完 ...

  2. [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)

    传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. [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:  ...

  7. [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$第一次 ...

  8. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  9. BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...

随机推荐

  1. uvm_globals——告诉这个世界我爱你

    uvm_globals.svh 存放全局的变量和方法.当UVM平台启动时,便在uvm_globals查找相应的方法,uvm_globals 的方法实现也比较简单,就是调用uvm_root对应的方法.其 ...

  2. Java多态的应用

    //多态的应用 class Animal{     public void eat(){     } } class Dog extends Animal{     public void eat() ...

  3. Spark中Java函数的使用方法笔记

    1: map 函数map是对RDD中的每个元素都执行一个指定的函数来产生一个新的RDD. 任何原RDD中的元素在新RDD中都有且只有一个元素与之对应. 2: mapPartitions函数</p ...

  4. codevs 3054 高精度练习-文件操作

    时间限制: 1 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题目描述 Description 输入一组数据,将每个数据加1后输出 输入描述 Input Descripti ...

  5. Python学习日志9月16日

    刚才我差点睡着了,差资料的时候太费神,有些累. 今天早晨学习了<head first HTML and CSS>,今天把昨天没看了的关于字体和颜色的一章节看完了,真长.我详细的做了笔记,并 ...

  6. UVA1665 Islands (并查集)

    补题,逆序考虑每个询问的时间,这样每次就变成出现新岛屿,然后用并查集合并统计.fa = -1表示没出现. 以前写过,但是几乎忘了,而且以前写得好丑的,虽然常数比较小,现在重新写练练手.每个单词后面都要 ...

  7. [洛谷P4556][BZOJ3307]雨天的尾巴-T3订正

    线段树合并+树上差分 题目链接(···) 「简单」「一般」——其实「一般」也只多一个离散化 考试时想法看>这里< 总思路:先存所有的操作,离散化,然后用树上差分解决修改,用权值线段树维护每 ...

  8. robotframe处理日志中文问题

    unicode('${addr1.text}',"utf-8")

  9. github更换仓库

    1.找到.git目录   2.打开config文件 3.修改仓库地址 4.重新提交 git push --all origin 这样就替我们的项目换仓啦!!!^_^   分类: git 参考资料: h ...

  10. github+hexo+themes搭建简易个性主题博客

    0x00  install Node.js and git 安装Node.js:http://www.runoob.com/nodejs/nodejs-install-setup.html 安装git ...