【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$第一次 ...
随机推荐
- linux nslookup-查询域名DNS信息的工具
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 nslookup命令是常用域名查询工具,就是查DNS信息用的命令. nslookup4有两种工作模式,即“交互模式”和“非交互模 ...
- 树莓派 - wiringPi
wiringPi其实和BCM2835 library类似,也是通过memmap, IOmap来实现在用户空间直接操作底层寄存器 wiringPi http://wiringpi.com/ Wiring ...
- Python和Java的语法对比,语法简洁上python的确完美胜出
Python是一种广泛使用的解释型.高级编程.通用型编程语言,由吉多·范罗苏姆创造,第一版发布于1991年.可以视之为一种改良(加入一些其他编程语言的优点,如面向对象)的LISP.Python的设计哲 ...
- sort cmp函数的写法 (特判排序 二级排序)
特判排序: 看看以下cmp写法,猜想作用: 该函数作用就是“将正数升序排列, 负数排到最后” 我认为cmp函数的一个特性就是, 如果return false, 那么函数就会将他们互换位置, retur ...
- java-得到字符串中出现次数最最多的字符,并打印出字符以及出现次数
最近面试总被面试到,整理出几种方式(有参考别人的部分) /** * java一个字符串中出现次数最多的字符以及次数 * @param args */ public static void main(S ...
- 张小龙最新内部演讲:KPI 是副产品,警惕复杂流程
张小龙最新内部演讲:KPI 是副产品,警惕复杂流程 各位 WXG(微信事业群)的同事们,大家早上好!又到我们一年一度的领导力大会. 大家都看到,我们微信团队膨胀还是比较快的,有 1500 多人了.对此 ...
- [codeforces471D]MUH and Cube Walls
[codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...
- Spring Data JPA 中常用注解
一.java对象与数据库字段转化 1.@Entity:标识实体类是JPA实体,告诉JPA在程序运行时生成实体类对应表 2.@Table:设置实体类在数据库所对应的表名 3.@Id:标识类里所在变量为主 ...
- jquery如何通过ajax请求获取后台数据显示在表格上
1.引入bootstrap和jquery的cdn <link rel="stylesheet" type="text/css" href="ht ...
- 详解SpringBoot集成jsp(附源码)+遇到的坑
本文介绍了SpringBoot集成jsp(附源码)+遇到的坑 ,分享给大家 1.大体步骤 (1)创建Maven web project: (2)在pom.xml文件添加依赖: (3)配置applica ...