洛谷—— P1849 [USACO12MAR]拖拉机Tractor
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 才能将拖拉机开会坐标原点。
输入输出样例
7 6 3
6 2
5 2
4 3
2 1
7 3
5 4
6 4
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的更多相关文章
- 洛谷 P1849 [USACO12MAR]拖拉机Tractor
题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...
- 洛谷P2698 [USACO12MAR]花盆Flowerpot
P2698 [USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and n ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]
题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...
- 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- [luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)
传送门 神奇的spfa #include <queue> #include <cstdio> #include <cstring> #include <ios ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
随机推荐
- 【全面】Linux基础知识和基本操作语句大全(一)
接触Linux已经有一段时间了,由于实际需要,三三两两地掌握了一些基本语法和实用语句,主要都是在日常开发中用得比较多的,条理不是特别清晰,请见谅!下面开始上硬货!! 基本操作: 关闭Linux系统的命 ...
- TTL与COMS的区别
1.电平的上限和下限定义不一样,CMOS具有更大的抗噪区域. 同是5伏供电的话,ttl一般是1.7V和3.5V的样子,CMOS一般是 2.2V,2.9V的样子,不准确,仅供参考. 2.电流驱动能力不 ...
- tar.xz结尾的文件的解压缩方法
例如: codeblocks-13.12-1_i386.debian.stable.tar 这个压缩包也是两层压缩,外面是xz压缩方式,里层是tar压缩方式. 解压缩方法: $xz -d ***.ta ...
- Win7通知区域的图标怎么去除?
由于本人有洁癖,最近在用win7的时候,很收不了已经卸载了的一些软件,在win7右下角的通知区域图标中还留有痕迹,于是上网查找了下解决方案. 用以下方法完美解决问题. 这里依然是以注册表的修改方法为主 ...
- CSS3中的border-radius兼容IE低版本解决方法
ie-css3.htc先说道说道这斯是弄啥嘞ie-css3.htc是一个可以让IE浏览器支持部份CSS3属性的htc文件,不只是box-shadow,它还可以让你的IE浏览器支持圆角属性border- ...
- pl/sql 函数及与存储过程的区别
函数用于返回特定的数据,当建立函数时,在函数头部必须包含return子句.而在函数体内必须包含return语句返回的数据.我们可以使用create function来建立函数. 1).接下来通过一个案 ...
- 一句话配置mongodb
一.安装mongodbmongodb-win32-x86_64-2008plus-ssl-3.2.9-signed.msi 二.以管理员身份,启动cmd命令mongod.exe --logpath & ...
- HDU-3718 Similarity
题目只有26个字母,所以我们新建一个二分图,v[i][j]表示字母i对应字母j时能成功匹配的个数,给这个边矩阵v求个最大匹配就是答案. #include <cstdlib> #includ ...
- IBM DB2 控制中心等图形工具在 Windows 下的字体设置
原文地址(直接看原文): http://loveseaside.iteye.com/blog/648941 [简介如下] IBM DB2 在版本 8.0 以上就提供了一个跨平台的基于 Java 的一套 ...
- java面试题之final、finalize和finally的区别
finally:finally是一个关键字,与try和catch一起用于异常的处理,finally块一定会执行,无论在try快中是否有发生异常. finalize:finalize方法是在对象被回收之 ...