https://www.luogu.org/problemnew/show/P1849

题目描述

After a long day of work, Farmer John completely forgot that he left his tractor in the middle of the field. His cows, always up to no good, decide to play a prank of Farmer John: they deposit N bales of hay (1 <= N <= 50,000) at various locations in the field, so that Farmer John cannot easily remove the tractor without first removing some of the bales of hay.

The location of the tractor, as well as the locations of the N hay bales, are all points in the 2D plane with integer coordinates in the range 1..1000. There is no hay bale located at the initial position of the tractor. When Farmer John drives his tractor, he can only move it in directions that are parallel to the coordinate axes (north, south, east, and west), and it must move in a sequence of integer amounts. For example, he might move north by 2 units, then east by 3 units. The tractor cannot move onto a point occupied by a hay bale.

Please help Farmer John determine the minimum number of hay bales he needs to remove so that he can free his tractor (that is, so he can drive his tractor to the origin of the 2D plane).

经过一天漫长的工作,农场主 John 完全忘记了他的拖拉机还在场地中央。他的奶牛们总喜欢和他搞些恶作剧,它们在场地的不同位置丢下 N(1 ≤ N ≤ 50,000)堆干草。这样 John 就必须先移走一些干草堆才能将拖拉机开走。

拖拉机和干草堆都可以看作是二维平面上的点,它们的坐标都是整数,坐标范围在 1 到1000 之间。没有那堆干草的坐标和拖拉机的初始坐标一致。John 驾驶拖拉机只能沿着坐标轴的方向移动若干单位长度,比如说,他可以先朝北移动 2 个单位长度,再向东移动 3 个单位长度等等。拖拉机不能移动到干草堆所占据的点。

请你帮助 John 计算一下,最少要移动多少堆干草才能将拖拉机开会坐标原点。

输入输出格式

输入格式:

第一行,三个用空格隔开的整数 N、x、y,表示有N 堆干草和拖拉机的起始坐标。

第 2行到第N+1 行,每行两个用空格隔开的整数 x、y,表示每堆干草的坐标。

输出格式:

一行一个整数,表示最少要移动多少堆干草 John 才能将拖拉机开会坐标原点。

输入输出样例

输入样例#1: 复制

7 6 3
6 2
5 2
4 3
2 1
7 3
5 4
6 4
输出样例#1: 复制

1 

注意边界
 #include <cstring>
#include <cstdio>
#include <queue> inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
} const int N(1e3+); struct Node {int x,y;}u,v;
std::queue<Node>que; int cost[N][N];
bool link[N][N],inq[N][N]; int fx[]={,,,-};
int fy[]={,,-,}; inline int SPFA()
{
memset(cost,/,sizeof(cost));
que.push(u); cost[u.x][u.y]=;
for(; !que.empty(); )
{
u=que.front(); inq[u.x][u.y]=; que.pop();
for(int i=; i<; ++i)
{
v.x=u.x+fx[i],v.y=u.y+fy[i];
if(v.x<||v.y<||v.x>||v.y>) continue;
if(cost[v.x][v.y]>cost[u.x][u.y]+link[v.x][v.y])
{
cost[v.x][v.y]=cost[u.x][u.y]+link[v.x][v.y];
if(!inq[v.x][v.y]) inq[v.x][v.y]=,que.push(v);
}
}
}
return cost[][];
} int Presist()
{
int n; read(n);
read(u.x),read(u.y);
for(int x,y,i=; i<=n; ++i)
read(x),read(y),link[x][y]=;
printf("%d\n",SPFA());
return ;
} int Aptal=Presist();
int main(int argc,char**argv){;}

洛谷—— P1849 [USACO12MAR]拖拉机Tractor的更多相关文章

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

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

  2. 洛谷P2698 [USACO12MAR]花盆Flowerpot

    P2698 [USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and n ...

  3. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  4. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]

    题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...

  5. 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  6. [luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)

    传送门 神奇的spfa #include <queue> #include <cstdio> #include <cstring> #include <ios ...

  7. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  8. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  9. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

随机推荐

  1. 将php数组转js数组,js如何接收PHP数组,json的用法

    首先下载下面这个文件(这是一段是别人写出来专门解析json的代码),然后引入这个文件! http://pan.baidu.com/s/1dD8qVr7 现在当我们需要用ajax与后台进行交互时,怎样将 ...

  2. Applied Nonparametric Statistics-lec7

    Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/9 经过前面的步骤,我们已经可以判断几个样本之间是否 ...

  3. iOS 中的视图函数 init initwithnib viewDidLoad viewWillAppear的总结

    我要总结的函数主要是这几个: UIView *view-如果view还没有被初始化的话,getter方法会先调用[self loadView],如果getter或者setter方法被重写了,子类中的g ...

  4. Linux学习-Shell的变量功能

    什么是变量? 简单的说,就是让某一个特定字串代表不固定的内容. 变量的可变性与方便性 举例来说,我们每个帐号的邮件信箱默认是以 MAIL 这个变量来进行存取的, 当 dmtsai 这个 使用者登陆时, ...

  5. POJ1719二分匹配

    第一次发文,就是一些学习的心得而已,自己忘得时候就可以来看看,好了废话不多说,直接说说这题的思路. 题意大概是每列只能射中一个白格子,每行可以射多个,让你输出这样的序列,明显的二分图求匹配,用列去选行 ...

  6. A+B问题的异常解法

    先%XZZ为敬 http://www.cnblogs.com/xzz_233/p/a-plus-b-problem.html 万恶之源:https://www.luogu.org/discuss/sh ...

  7. 服务器迁移至Linux操作系统

    我在这里试了ubuntu.Debian,centos.最终还是选择了centos 使用工具putty,远程桌面的话使用vnc viewer(看起来service文件更改只需要替换user,但是路径不对 ...

  8. HDU-1529 Cashier Employment

    据网上说这是到差分约束四星题... 可我觉得难吗? 比推DP方程容易... 两种约束方式,当然实现到程序就变成六种了... #include <cstdio> #include <c ...

  9. BZOJ1856 [SCOI2010]生成字符串 【组合数】

    题目 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgww想要知道满足要求 ...

  10. 不支持模块化规范的插件可以使用import 导入的原因

    模块化当中的模块其实是个闭包,然后导出这个闭包,这个是为了解决全局变量污染的问题的. 所以模块当中直接定义的变量 比如  var foo = 0; 这个并不会是全局变量,而是当前模块闭包当中的局部变量 ...