【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$第一次 ...
随机推荐
- python 连接sqlserver: pymssql
停了一个月,终于还是把这个做了,工作需要!!!在装pymssql时,一直报错,确定了要先装freetds: 1. 安装freetds时报错,搜索到要先进行如下操作: brew unlink freet ...
- pyton学习之路
文件操作 打开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内容:] a,追加模式.[可读: 不存在则创建:存在则只追加内容:] "+" ...
- Mysql学习总结(43)——MySQL主从复制详细配置
环境 操作系统:CentOS-6.6-x86_64-bin-DVD1.iso MySQL版本:mysql-5.6.26.tar.gz 主节点IP:192.168.1.205 主机名:edu-mysql ...
- two strings
A1484. two strings(罗干) 时间限制:1.0s 内存限制:256.0MB [问题描述] 给定两个字符串A和B,有五种操作,操作1为在A串开头添加一个字符,操作2为在A串结尾添加一 ...
- MySQL最优配置文件模板·2016-11-28
小伙伴们大爱的MySQL最优配置文件模板更新啦.对之前的MySQL最优配置文件·20160901做了一些修正,更为名至实归.可以通过此链接进行下载.当然,更欢迎同学们提出意见和建议,共同打造一个最优M ...
- add favorite & 收藏夹
add favorite // 收藏夹 function favorite (){ var ctrl = (navigator.userAgent.toLowerCase()).indexOf(&qu ...
- [K/3Cloud] 单据新增、复制、新增行、复制行的过程
整单复制:先执行CopyData(获得数据包),在执行AfterCreateNewData(可处理数据包),不会执行AfterCreateNewEntryRow 单据新增:先执行AfterCreate ...
- android开发里跳过的坑——adb connect连不上
user版本在系统init.rc里已经添加了setprop service.adb.tcp.port 5555 ,但是刷机以后,发现adb connect怎么都连不上,重启电脑,改变网络,巴拉巴拉,能 ...
- Form表单的action和onSubmit示例介绍
action是form的属性,onSubmit为事件,要说执行的先后顺序,个人理解是onSubmit在先. 第一:action是form的属性,html5已经将其定义为必需的属性值,onSubmit为 ...
- hdu - 1104 Remainder (bfs + 数论)
http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需 ...