【题目链接】

点击打开链接

【算法】

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. linux nslookup-查询域名DNS信息的工具

    博主推荐:更多网络测试相关命令关注 网络测试  收藏linux命令大全 nslookup命令是常用域名查询工具,就是查DNS信息用的命令. nslookup4有两种工作模式,即“交互模式”和“非交互模 ...

  2. 树莓派 - wiringPi

    wiringPi其实和BCM2835 library类似,也是通过memmap, IOmap来实现在用户空间直接操作底层寄存器 wiringPi http://wiringpi.com/ Wiring ...

  3. Python和Java的语法对比,语法简洁上python的确完美胜出

    Python是一种广泛使用的解释型.高级编程.通用型编程语言,由吉多·范罗苏姆创造,第一版发布于1991年.可以视之为一种改良(加入一些其他编程语言的优点,如面向对象)的LISP.Python的设计哲 ...

  4. sort cmp函数的写法 (特判排序 二级排序)

    特判排序: 看看以下cmp写法,猜想作用: 该函数作用就是“将正数升序排列, 负数排到最后” 我认为cmp函数的一个特性就是, 如果return false, 那么函数就会将他们互换位置, retur ...

  5. java-得到字符串中出现次数最最多的字符,并打印出字符以及出现次数

    最近面试总被面试到,整理出几种方式(有参考别人的部分) /** * java一个字符串中出现次数最多的字符以及次数 * @param args */ public static void main(S ...

  6. 张小龙最新内部演讲:KPI 是副产品,警惕复杂流程

    张小龙最新内部演讲:KPI 是副产品,警惕复杂流程 各位 WXG(微信事业群)的同事们,大家早上好!又到我们一年一度的领导力大会. 大家都看到,我们微信团队膨胀还是比较快的,有 1500 多人了.对此 ...

  7. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

  8. Spring Data JPA 中常用注解

    一.java对象与数据库字段转化 1.@Entity:标识实体类是JPA实体,告诉JPA在程序运行时生成实体类对应表 2.@Table:设置实体类在数据库所对应的表名 3.@Id:标识类里所在变量为主 ...

  9. jquery如何通过ajax请求获取后台数据显示在表格上

    1.引入bootstrap和jquery的cdn <link rel="stylesheet" type="text/css" href="ht ...

  10. 详解SpringBoot集成jsp(附源码)+遇到的坑

    本文介绍了SpringBoot集成jsp(附源码)+遇到的坑 ,分享给大家 1.大体步骤 (1)创建Maven web project: (2)在pom.xml文件添加依赖: (3)配置applica ...