【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(30)--文件系统:介绍一个高大上的东西
知识点 os,os.path模块中关于文件.目录常用的函数使用方法 在使用os模块,需要先进行import操作: import os os模块中关于文件/目录常用的函数使用方法 函数名 函数作用 示例 ...
- lucene-5.3.1配置(win7x64)
lucene下载地址:http://www.us.apache.org/dist/lucene/java/5.3.1/lucene-5.3.1.zip 下载之后解压 控制台应用程序下配置: 找到luc ...
- python链家网高并发异步爬虫and异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- python视频 神经网络 Tensorflow
python视频 神经网络 Tensorflow 模块 视频教程 (带源码) 所属网站分类: 资源下载 > python视频教程 作者:smile 链接:http://www.pythonhei ...
- PID28 [Stupid]愚蠢的宠物
题链:https://www.rqnoj.cn/problem/28 题目描述 背景 大家都知道,sheep有两只可爱的宠物(一只叫神牛,一只叫神菜).有一天,sheep带着两只宠物到狗狗家时,这两只 ...
- popup介绍
一.作用 用于使浏览器自动生成弹窗 二.示例 1.新建Django项目,新建APP:app01, 项目根目录下新建文件夹static 2.静态文件配置,在settings.py中配置static: 3 ...
- 59. Spring Boot Validator校验【从零开始学Spring Boot】
大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...
- noip模拟赛 Nephren Ruq Insania
题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg 注意:本题大样例4的输出文件修改为 https://pan.baidu.com/s/1b ...
- noip模拟赛 集合
分析:感觉像是贪心,再看数据范围这么大,肯定是贪心没错.但是要怎么贪呢?主要的思想是让每次往上加的数尽量多,肯定要先把0分裂,如果能正好一起跳到最终状态就好.举个例子:5,3,2,1,最大值比次大值大 ...
- HAProxy+Redis实现负载负载均衡(待实践)
为什么要使用HA,原因是可以聚合出一个VIP,也就是可以使用单一IP来访问下面多个Redis的实例. 首先说明一下,如果基于3.0以后搭建的官方原始Redis Cluster方案,使用HAProxy是 ...