[luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)
神奇的spfa
- #include <queue>
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #define N 1010
- #define max(x, y) ((x) > (y) ? (x) : (y))
- int n, mx, my;
- int dis[N][N];
- bool map[N][N], vis[N][N];
- int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
- struct node
- {
- int x, y;
- node(int x = 0, int y = 0) : x(x), y(y) {}
- };
- std::queue <node> q;
- inline int read()
- {
- int x = 0, f = 1;
- char ch = getchar();
- for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
- for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
- return x * f;
- }
- int main()
- {
- node u, v;
- int i, x, y;
- n = read();
- x = read();
- y = read();
- memset(dis, 127, sizeof(dis));
- dis[x][y] = 0;
- q.push(node(x, y));
- for(i = 1; i <= n; i++)
- {
- x = read();
- y = read();
- map[x][y] = 1;
- mx = max(mx, x);
- my = max(my, y);
- }
- while(!q.empty())
- {
- u = q.front();
- q.pop();
- vis[u.x][u.y] = 0;
- for(i = 0; i < 4; i++)
- {
- x = u.x + dx[i];
- y = u.y + dy[i];
- if(x >= 0 && x <= mx + 1 && y >= 0 && y <= my + 1 && dis[x][y] > dis[u.x][u.y] + map[x][y])
- {
- dis[x][y] = dis[u.x][u.y] + map[x][y];
- if(!vis[x][y])
- {
- vis[x][y] = 1;
- q.push(node(x, y));
- }
- }
- }
- }
- printf("%d\n", dis[1][1]);
- return 0;
- }
[luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)的更多相关文章
- 洛谷 P1849 [USACO12MAR]拖拉机Tractor
题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...
- 洛谷—— P1849 [USACO12MAR]拖拉机Tractor
https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...
- [USACO12MAR]拖拉机
题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...
- USACO Tractor
洛谷 P3073 [USACO13FEB]拖拉机Tractor 洛谷传送门 JDOJ 2351: USACO 2013 Feb Silver 2.Tractor JDOJ传送门 题意翻译 题目描述 F ...
- ImageNet2017文件下载
ImageNet2017文件下载 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PASCAL ...
- ImageNet2017文件介绍及使用
ImageNet2017文件介绍及使用 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PAS ...
- USACO 2012 March Silver Tractor /// 优先队列BFS oj21567
题目大意: 输入n,(x,y):n为阻挡的草堆数量,(x,y)为开始时拖拉机所在的位置 接下来n行每行一个坐标(a,b):为各个草堆的坐标 输出拖拉机要回到原点(0,0)需要移动的草堆数量 Sampl ...
- 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa
3627: [JLOI2014]路径规划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 186 Solved: 70[Submit][Status] ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
随机推荐
- HTML5实现页面自动跳转
初学H5,书上说的是<meta http-equiv="refresh" content="5“ url=”./error.html"/>这样可以实 ...
- String的用法——构造方法
package cn.itcast_01; /* 字符串:就是多个字符组成的一串数据,也可以看成一个字符数组 *通过API,我们得知: A: 字符串字面值,如abc,也可以看做一个对象 B:字符串是常 ...
- 通过流传入excel解析的问题
做了个excel文件导入的功能,接收excel文件流,先读取文件流头部信息确定文件类型,然后调用poi方法 OPCPackage pkg = OPCPackage.open(inputStream) ...
- poj1724 ROADS
题意: N个城市,编号1到N.城市间有R条单向道路.每条道路连接两个城市,有长度和过路费两个属性.Bob只有K块钱,他想从城市1走到城市N.问最短共需要走多长的路.如果到不了N,输出-12<=N ...
- Intro Of Myself
- 8 Explicit Animations 指明的动画 笔记
8 Explicit Animations 指明的动画 笔记 If you want something done right, do it yourself. 如果你想让事情做好,那就自动来 ...
- break,continue,return的区别
break,continue,return的区别 break 当break语句用于循环语句时,会终止执行循环,并执行循环后代码(如果有的话). function main() { for(var i ...
- docker 深入理解之cgroups
cgroups 资源限制 cgroups 是什么 cgroups 最初名为process container,有Google工程师Paul Menage和Rohit Seth于 2006 年提出,后由 ...
- centos7 搭建jenkins
centos7 搭建jenkins.note 环境:VMware 虚拟机 centos 7+ jdk 1.8+ tomcat7+jenkins搭建好linux 服务器后,关闭防火墙 停止firewal ...
- Opencascade 选择器算法
算法的阶段 该算法包括预处理和三个主要阶段. 使用深度优先搜索逐层遍历所有对象 . 预处理 计算平截头体及其主要特征的计算. 第一阶段 - 遍历第一级BVH树 在成功构建选择平截头体之后,算法开始遍历 ...