[BZOJ4992] [Usaco2017 Feb]Why Did the Cow Cross the Road(spfa)
把每个点和曼哈顿距离距离它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)的更多相关文章
- BZOJ4992 [Usaco2017 Feb]Why Did the Cow Cross the Road 最短路 SPFA
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4992 题意概括 在一幅n*n的地图上,Amber从左上角走到右下角,每走一步需要花费时间t,每走完 ...
- [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)
传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...
- 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 ...
随机推荐
- docker 配置国内镜像源 linux/mac/windows
部分内容来自:http://guide.daocloud.io/dcs/daocloud-9153151.html 加速器官方DaoCloud承诺:加速器服务永久免费且无流量限制 使用前提:注册Dao ...
- IOSButton自定义
+ (APCCustomBackButton *)customBackButtonWithTarget:(id)aTarget action:(SEL)anAction tintColor:(UICo ...
- 正则表达说明—Pattern API
字符类 [abc] 匹配a.b.c任意一个字符 [^abc] 匹配除了a.b.c外的任意一个字符 [a-zA-Z] 匹配a-z或A ...
- SQL语句,mysql数据库
sql语句,把一张表里的数据,和特定数据(固定常量)新插入另一张表 ,,, from wm_jobpoint INSERT INTO wm_department(departmentcode,depa ...
- FZU 1977 Pandora adventure (插头DP,常规)
题意:有一个n*m矩阵,其中有些格子必走,有些格子不可走,其他格子是可走也可不走,问有多少条哈密顿回路? 思路: 本来是一道很简单的题,代码写多了连白痴bug都查不出了,竟然用i>=ex& ...
- (九)mybatis之生命周期
生命周期 SqlSessionFactoryBuilder SqlSessionFactoryBuilder的作用就是生成SqlSessionFactory对象,是一个构建器.所以我们一旦构建 ...
- 单源最短路Dijstra
#include<iostream> #include<cstring> #define INF 0x3f3f3f3f using namespace std; ][],d[] ...
- 解决wpf popup控件遮挡其他程序的问题
public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.Top ...
- Linux 标准 I/O 库
为什么要设计标准 I/O 库? 直接使用 API 进行文件访问时,需要考虑许多细节问题,例如:read . write 时,缓冲区的大小该如何确定,才能使效率最优 read 和 write 等底层系统 ...
- Window命令行杀进程
Window命令行杀进程 1.查看任务列表 tasklist 2.以映象名杀 taskkill -t -f -im xx.exe 3.以进程杀死 taskkill /pid pid号 /f 4.针对w ...