【题目链接】

点击打开链接

【算法】

dist[i][j][k]表示当前走到(i,j),走的步数除以3的余数为k的最小花费

spfa即可

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 110
const int INF = 1e9; struct info
{
int x,y,s;
}; const int dx[] = {,,-,};
const int dy[] = {-,,,}; int i,j,n,t;
int val[MAXN][MAXN]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < )
{
putchar('-');
x = -x;
}
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
}
bool ok(int x,int y)
{
return x >= && x <= n && y >= && y <= n;
}
inline void spfa()
{
int i,j,tx,ty,ans;
queue< info > q;
static int dist[MAXN][MAXN][],inq[MAXN][MAXN][];
info cur;
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
dist[i][j][] = dist[i][j][] = dist[i][j][] = INF;
}
}
dist[][][] = ;
inq[][][] = ;
q.push((info){,,});
while (!q.empty())
{
cur = q.front();
inq[cur.x][cur.y][cur.s] = ;
q.pop();
for (i = ; i < ; i++)
{
tx = cur.x + dx[i];
ty = cur.y + dy[i];
if (ok(tx,ty))
{
if (!cur.s)
{
if (dist[cur.x][cur.y][] + t < dist[tx][ty][])
{
dist[tx][ty][] = dist[cur.x][cur.y][] + t;
if (!inq[tx][ty][])
{
inq[tx][ty][] = ;
q.push((info){tx,ty,});
}
}
}
if (cur.s == )
{
if (dist[cur.x][cur.y][] + t < dist[tx][ty][])
{
dist[tx][ty][] = dist[cur.x][cur.y][] + t;
if (!inq[tx][ty][])
{
inq[tx][ty][] = ;
q.push((info){tx,ty,});
}
}
}
if (cur.s == )
{
if (dist[cur.x][cur.y][] + val[tx][ty] + t < dist[tx][ty][])
{
dist[tx][ty][] = dist[cur.x][cur.y][] + val[tx][ty] + t;
if (!inq[tx][ty][])
{
inq[tx][ty][] = ;
q.push((info){tx,ty,});
}
}
}
}
}
}
ans = min(min(dist[n][n][],dist[n][n][]),dist[n][n][]);
writeln(ans);
} int main() { read(n); read(t); for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
read(val[i][j]);
}
}
spfa(); return ; }

【USACO 2017Feb】 Why Did the Cow Cross the Road的更多相关文章

  1. 【USACO 2017FEB】 Why Did the Cow Cross the Road III

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...

  2. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  3. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

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

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

  6. 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

    //神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...

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

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

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

随机推荐

  1. python3.x Day6 多线程

    线程???进程????区别???何时使用??? 进程:是程序以一个整体的形式暴露给操作系统管理,里边包含了对各种资源的调用,内存的使用,对各种资源的管理的集合,这就叫进程 线程:是操作系统最小的调度单 ...

  2. list & dictionary

    list不能直接进行对应,dictionary可以 list用[],dictionary用{}

  3. hdu 1698区间延迟更新

    #include<stdio.h> #define N 100100 struct node { int x,y,yanchi; }a[N*4];//注意数组范围 void build(i ...

  4. 听dalao讲课 7.26

    XFZ今天讲了些关于多项式求ln和多项式求导以及多项式求积分的东西 作为一个连导数和积分根本就不会的蒟蒻,就像在听天书,所以不得不补点前置知识 1.积分 积分是微积分学与数学分析里的一个核心概念.通常 ...

  5. Linux下汇编语言学习笔记24 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  6. html上传图片类型

    <html>  <head>  <meta charset="utf-8">  <title>上传图片</title> ...

  7. elk实时日志分析平台部署搭建详细实现过程

    原文:http://blog.csdn.net/mchdba/article/details/52132663 1.ELK平台介绍 在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段:以下内容 ...

  8. Android Studio签名打包应用

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50812391 可直接看看以下的Android Studio中签名应用 Android要 ...

  9. Android Wear - App Structure for Android Wear(应用结构)

    ---------------------------------------------------------------------------------------------------- ...

  10. web前端减少你按刷新的频率

    Browsersync 先下载:nodejs  ,然后安装完以后,我们在命令行打印 node -v 完成后我们进行以下操作,安装browser-sync ,官网上有详细的教程,请访问:Browsers ...