传送门

神奇的spfa

  1. #include <queue>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #define N 1010
  6. #define max(x, y) ((x) > (y) ? (x) : (y))
  7.  
  8. int n, mx, my;
  9. int dis[N][N];
  10. bool map[N][N], vis[N][N];
  11. int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
  12.  
  13. struct node
  14. {
  15. int x, y;
  16. node(int x = 0, int y = 0) : x(x), y(y) {}
  17. };
  18.  
  19. std::queue <node> q;
  20.  
  21. inline int read()
  22. {
  23. int x = 0, f = 1;
  24. char ch = getchar();
  25. for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
  26. for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
  27. return x * f;
  28. }
  29.  
  30. int main()
  31. {
  32. node u, v;
  33. int i, x, y;
  34. n = read();
  35. x = read();
  36. y = read();
  37. memset(dis, 127, sizeof(dis));
  38. dis[x][y] = 0;
  39. q.push(node(x, y));
  40. for(i = 1; i <= n; i++)
  41. {
  42. x = read();
  43. y = read();
  44. map[x][y] = 1;
  45. mx = max(mx, x);
  46. my = max(my, y);
  47. }
  48. while(!q.empty())
  49. {
  50. u = q.front();
  51. q.pop();
  52. vis[u.x][u.y] = 0;
  53. for(i = 0; i < 4; i++)
  54. {
  55. x = u.x + dx[i];
  56. y = u.y + dy[i];
  57. if(x >= 0 && x <= mx + 1 && y >= 0 && y <= my + 1 && dis[x][y] > dis[u.x][u.y] + map[x][y])
  58. {
  59. dis[x][y] = dis[u.x][u.y] + map[x][y];
  60. if(!vis[x][y])
  61. {
  62. vis[x][y] = 1;
  63. q.push(node(x, y));
  64. }
  65. }
  66. }
  67. }
  68. printf("%d\n", dis[1][1]);
  69. return 0;
  70. }

  

[luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)的更多相关文章

  1. 洛谷 P1849 [USACO12MAR]拖拉机Tractor

    题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...

  2. 洛谷—— P1849 [USACO12MAR]拖拉机Tractor

    https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...

  3. [USACO12MAR]拖拉机

    题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...

  4. USACO Tractor

    洛谷 P3073 [USACO13FEB]拖拉机Tractor 洛谷传送门 JDOJ 2351: USACO 2013 Feb Silver 2.Tractor JDOJ传送门 题意翻译 题目描述 F ...

  5. ImageNet2017文件下载

    ImageNet2017文件下载 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PASCAL ...

  6. ImageNet2017文件介绍及使用

    ImageNet2017文件介绍及使用 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PAS ...

  7. USACO 2012 March Silver Tractor /// 优先队列BFS oj21567

    题目大意: 输入n,(x,y):n为阻挡的草堆数量,(x,y)为开始时拖拉机所在的位置 接下来n行每行一个坐标(a,b):为各个草堆的坐标 输出拖拉机要回到原点(0,0)需要移动的草堆数量 Sampl ...

  8. 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa

    3627: [JLOI2014]路径规划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 186  Solved: 70[Submit][Status] ...

  9. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

随机推荐

  1. HTML5实现页面自动跳转

    初学H5,书上说的是<meta http-equiv="refresh" content="5“ url=”./error.html"/>这样可以实 ...

  2. String的用法——构造方法

    package cn.itcast_01; /* 字符串:就是多个字符组成的一串数据,也可以看成一个字符数组 *通过API,我们得知: A: 字符串字面值,如abc,也可以看做一个对象 B:字符串是常 ...

  3. 通过流传入excel解析的问题

    做了个excel文件导入的功能,接收excel文件流,先读取文件流头部信息确定文件类型,然后调用poi方法 OPCPackage pkg = OPCPackage.open(inputStream) ...

  4. poj1724 ROADS

    题意: N个城市,编号1到N.城市间有R条单向道路.每条道路连接两个城市,有长度和过路费两个属性.Bob只有K块钱,他想从城市1走到城市N.问最短共需要走多长的路.如果到不了N,输出-12<=N ...

  5. Intro Of Myself

  6. 8 Explicit Animations 指明的动画 笔记

    8 Explicit Animations 指明的动画 笔记     If you want something done right, do it yourself. 如果你想让事情做好,那就自动来 ...

  7. break,continue,return的区别

    break,continue,return的区别 break 当break语句用于循环语句时,会终止执行循环,并执行循环后代码(如果有的话). function main() { for(var i ...

  8. docker 深入理解之cgroups

    cgroups 资源限制 cgroups 是什么 cgroups 最初名为process container,有Google工程师Paul Menage和Rohit Seth于 2006 年提出,后由 ...

  9. centos7 搭建jenkins

    centos7 搭建jenkins.note 环境:VMware 虚拟机 centos 7+ jdk 1.8+ tomcat7+jenkins搭建好linux 服务器后,关闭防火墙 停止firewal ...

  10. Opencascade 选择器算法

    算法的阶段 该算法包括预处理和三个主要阶段. 使用深度优先搜索逐层遍历所有对象 . 预处理 计算平截头体及其主要特征的计算. 第一阶段 - 遍历第一级BVH树 在成功构建选择平截头体之后,算法开始遍历 ...