【USACO 2017Feb】 Why Did the Cow Cross the Road
【题目链接】
【算法】
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的更多相关文章
- 【USACO 2017FEB】 Why Did the Cow Cross the Road III
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...
- [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)
\(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...
- Why Did the Cow Cross the Road III(树状数组)
Why Did the Cow Cross the Road III 时间限制: 1 Sec 内存限制: 128 MB提交: 65 解决: 28[提交][状态][讨论版] 题目描述 The lay ...
- 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 ...
- 洛谷 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 ...
- [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$第一次 ...
随机推荐
- Caused by: java.io.IOException: 您的主机中的软件中止了一个已建立的连接。
异常详情 2017-07-16 10:55:26,218 ERROR [500.jsp] - java.io.IOException: 你的主机中的软件中止了一个已建立的连接. org.apache. ...
- cookie、localStorage和sessionStorage三者的异同
1.三者都是用来存储浏览器数据的 2.生命周期:cookie可以设置数据失效时间,默认是关闭浏览器后失效:localStorage中的数据是永久保存的,除非手动清除:sessionStorage的数据 ...
- MVC系统学习5——验证
其实关于Mvc的验证在上一篇已经有讲过一些了,可以通过在我们定义的Model上面添加相应的System.ComponentModel.DataAnnotations空间下的验证属性.在服务器端通过Mo ...
- HDU-1597find the nth digit,超短代码一遍过,啦啦啦啦~~
find the nth digit Time Limit: 10 ...
- 【bzoj1922】[Sdoi2010]大陆争霸 - STL - dijkstra
信仰斯普林·布拉泽的克里斯国教徒. 幻想历 8012年 3月2日,位于杰森国东部小镇神谕镇的克里斯国教徒发动 起义. 幻想历 8012年 3月7日,神谕镇的起义被杰森国大军以残酷手段镇压. 幻想历 8 ...
- 一个1x1px大小Data/Base64数据的gif透明图片
<img src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNg ...
- Layui动画、按钮、表单
Layui动画.按钮.表单 在实用价值的前提之下,我们并没有内置过多花俏的动画.而他们同样在 layui 的许多交互元素中,发挥着重要的作用.layui 的动画全部采用 CSS3,因此不支持ie8和部 ...
- MongoDB小结02 - 配置、启动MongoDB
下载MongoDB 第一步:登上MongoDB官网,找到自己的适合的版本下载 第二步:解压(免安装),改名mongodb(举例命名,可以任个人喜好),放在你喜欢的位置(任喜好) 第三步:通过命令行: ...
- 具体解说Android图片下载框架UniversialImageLoader之内存缓存(三)
前面的两篇文章着重介绍的是磁盘缓存,这篇文章主要是解说一下内存缓存.对于内存缓存.也打算分两篇文章来进行解说.在这一篇文章中,我们主要是关注三个类, MemoryCache.BaseMemoryCac ...
- Centos7最小安装下Install Clamav(2017-06-09最后更新)
If you are installing ClamAV for the first time, you have to add a new user and group to your system ...