【题目链接】

点击打开链接

【算法】

dist[i][j][k]表示当前走到(i,j),走的步数除以3的余数为k的最小花费

spfa即可

【代码】

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAXN 110
  4. const int INF = 1e9;
  5.  
  6. struct info
  7. {
  8. int x,y,s;
  9. };
  10.  
  11. const int dx[] = {,,-,};
  12. const int dy[] = {-,,,};
  13.  
  14. int i,j,n,t;
  15. int val[MAXN][MAXN];
  16.  
  17. template <typename T> inline void read(T &x)
  18. {
  19. int f = ; x = ;
  20. char c = getchar();
  21. for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
  22. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  23. x *= f;
  24. }
  25. template <typename T> inline void write(T x)
  26. {
  27. if (x < )
  28. {
  29. putchar('-');
  30. x = -x;
  31. }
  32. if (x > ) write(x/);
  33. putchar(x%+'');
  34. }
  35. template <typename T> inline void writeln(T x)
  36. {
  37. write(x);
  38. puts("");
  39. }
  40. bool ok(int x,int y)
  41. {
  42. return x >= && x <= n && y >= && y <= n;
  43. }
  44. inline void spfa()
  45. {
  46. int i,j,tx,ty,ans;
  47. queue< info > q;
  48. static int dist[MAXN][MAXN][],inq[MAXN][MAXN][];
  49. info cur;
  50. for (i = ; i <= n; i++)
  51. {
  52. for (j = ; j <= n; j++)
  53. {
  54. dist[i][j][] = dist[i][j][] = dist[i][j][] = INF;
  55. }
  56. }
  57. dist[][][] = ;
  58. inq[][][] = ;
  59. q.push((info){,,});
  60. while (!q.empty())
  61. {
  62. cur = q.front();
  63. inq[cur.x][cur.y][cur.s] = ;
  64. q.pop();
  65. for (i = ; i < ; i++)
  66. {
  67. tx = cur.x + dx[i];
  68. ty = cur.y + dy[i];
  69. if (ok(tx,ty))
  70. {
  71. if (!cur.s)
  72. {
  73. if (dist[cur.x][cur.y][] + t < dist[tx][ty][])
  74. {
  75. dist[tx][ty][] = dist[cur.x][cur.y][] + t;
  76. if (!inq[tx][ty][])
  77. {
  78. inq[tx][ty][] = ;
  79. q.push((info){tx,ty,});
  80. }
  81. }
  82. }
  83. if (cur.s == )
  84. {
  85. if (dist[cur.x][cur.y][] + t < dist[tx][ty][])
  86. {
  87. dist[tx][ty][] = dist[cur.x][cur.y][] + t;
  88. if (!inq[tx][ty][])
  89. {
  90. inq[tx][ty][] = ;
  91. q.push((info){tx,ty,});
  92. }
  93. }
  94. }
  95. if (cur.s == )
  96. {
  97. if (dist[cur.x][cur.y][] + val[tx][ty] + t < dist[tx][ty][])
  98. {
  99. dist[tx][ty][] = dist[cur.x][cur.y][] + val[tx][ty] + t;
  100. if (!inq[tx][ty][])
  101. {
  102. inq[tx][ty][] = ;
  103. q.push((info){tx,ty,});
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. ans = min(min(dist[n][n][],dist[n][n][]),dist[n][n][]);
  111. writeln(ans);
  112. }
  113.  
  114. int main() {
  115.  
  116. read(n); read(t);
  117.  
  118. for (i = ; i <= n; i++)
  119. {
  120. for (j = ; j <= n; j++)
  121. {
  122. read(val[i][j]);
  123. }
  124. }
  125. spfa();
  126.  
  127. return ;
  128.  
  129. }

【USACO 2017Feb】 Why Did the Cow Cross the Road的更多相关文章

  1. 【USACO 2017FEB】 Why Did the Cow Cross the Road III

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...

  2. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  3. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

  4. 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 ...

  5. 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 ...

  6. 洛谷 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 ...

  7. [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 ...

  8. [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:  ...

  9. [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$第一次 ...

随机推荐

  1. 零基础入门学习Python(30)--文件系统:介绍一个高大上的东西

    知识点 os,os.path模块中关于文件.目录常用的函数使用方法 在使用os模块,需要先进行import操作: import os os模块中关于文件/目录常用的函数使用方法 函数名 函数作用 示例 ...

  2. lucene-5.3.1配置(win7x64)

    lucene下载地址:http://www.us.apache.org/dist/lucene/java/5.3.1/lucene-5.3.1.zip 下载之后解压 控制台应用程序下配置: 找到luc ...

  3. python链家网高并发异步爬虫and异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  4. python视频 神经网络 Tensorflow

    python视频 神经网络 Tensorflow 模块 视频教程 (带源码) 所属网站分类: 资源下载 > python视频教程 作者:smile 链接:http://www.pythonhei ...

  5. PID28 [Stupid]愚蠢的宠物

    题链:https://www.rqnoj.cn/problem/28 题目描述 背景 大家都知道,sheep有两只可爱的宠物(一只叫神牛,一只叫神菜).有一天,sheep带着两只宠物到狗狗家时,这两只 ...

  6. popup介绍

    一.作用 用于使浏览器自动生成弹窗 二.示例 1.新建Django项目,新建APP:app01, 项目根目录下新建文件夹static 2.静态文件配置,在settings.py中配置static: 3 ...

  7. 59. Spring Boot Validator校验【从零开始学Spring Boot】

    大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...

  8. noip模拟赛 Nephren Ruq Insania

    题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg 注意:本题大样例4的输出文件修改为 https://pan.baidu.com/s/1b ...

  9. noip模拟赛 集合

    分析:感觉像是贪心,再看数据范围这么大,肯定是贪心没错.但是要怎么贪呢?主要的思想是让每次往上加的数尽量多,肯定要先把0分裂,如果能正好一起跳到最终状态就好.举个例子:5,3,2,1,最大值比次大值大 ...

  10. HAProxy+Redis实现负载负载均衡(待实践)

    为什么要使用HA,原因是可以聚合出一个VIP,也就是可以使用单一IP来访问下面多个Redis的实例. 首先说明一下,如果基于3.0以后搭建的官方原始Redis Cluster方案,使用HAProxy是 ...