【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$第一次 ...
随机推荐
- LAMP 服务器环境
学习PHP脚本编程语言之前,必须先搭建并熟悉开发环境,开发环境有很多种,例如LAMP.WAMP.MAMP等.这里我介绍一下LAMP环境的搭建,即Linux.Apache.MySQL.PHP环境. 一. ...
- 简述FTP主动模式与被动模式
1 FTP工作模式 2 不同模式FTP面临的问题 3 主动模式的FTP连接建立连接主要步骤 客户端打开一个随机的端口(端口号大于1024,在这里,我们称它为x),同时一个FTP进程连接至服务器的21号 ...
- mysql 删除数据重复的记录
delete from user where id not in ( select * from ( select min(id) from user group by username,email ...
- WinMain名词解析
WinMain程序名词解析 int WINAPI WinMain(HINSTANCE hInstance ,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nS ...
- vscode调试angular2
调试步骤: 1.安装nodejs 2.安装vscode 3.vscode安装debugger for chrome插件 4.选择调试->打开调试配置,选择chrome配置,打开lauch.jso ...
- Vue如何引入icon图标
1.下载icon图标,推荐icomoon网站,里面有大量的矢量图标,也可以自定义,当然你也可以去阿里巴巴矢量图标库下载你所需要的小图标.点击进入icomoon网站点击右上角“IcoM ...
- Ajax的特点
[传统提交方式] 客户端提交请求后,服务器会找到相应的资源进行执行.并将执行结果重新发送给客户端.客户端接收到服务器端的响应会进行重新解释并显示.此时的页面是一个全新的页面. [Ajax提交] 客户端 ...
- CodeForcesGym 100512D Dynamic LCA
Dynamic LCA Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...
- [HAOI2011]Problem b 题解
题目大意: 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y)=k. 思路: 设f(k)为当1≤x≤n,1≤y≤m,且n≤m,使gcd(x,y)=k的数对 ...
- [NOIP2007] 提高组 洛谷P1098 字符串的展开
题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母获数 ...