洛谷—— 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方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
随机推荐
- 如何在微信中发送"相册"文件时有选择性地显示视频文件
相信很多微信用户在使用微信给朋友,同事发送相册中的文件时,微信会显示你手机中的视频文件,这样很不方便. 如果要完全不显示视频文件: 随便在手机中建立一个文件夹,名字叫 ".nomedia&q ...
- how to setting a i2c driver
How to instantiate I2C devices============================== Unlike PCI or USB devices, I2C devices ...
- 欧拉函数:HDU1787-GCD Again(欧拉函数的模板)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- CSS(非布局样式)
CSS(非布局样式) 问题1.CSS样式(选择器)的优先级 1.计算权重 2.!important 3.内联样式比外嵌样式高 4.后写的优先级高 问题2.雪碧图的作用 1.减少 HTTP 请求数,提高 ...
- 【ZABBIX】Linux下安装ZABBIX
说明:搭建ZABBIX所需的软件列表为:RHEL6.5+Nginx+MySQL+PHP+ZABBIX. 一.软件包 软件名称 版本 下载地址 nginx 1.10.3 http://nginx.org ...
- 【Netty】NIO框架Netty入门
Netty介绍 Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 也就是说,Netty ...
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- Struts2报错:No result defined for action xxx and result input
case如下: 1. 后台程序要升级, 修改了一些功能,但是没有修改或者添加action的参数. 2. 数据库需要升级,执行了一些sql,修改过action的值. 3. 当修改某个已经存在的记录,然后 ...
- POJ 1953 World Cup Noise
World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14397 Accepted: 7129 ...
- 简单实用jstl实现“登录|注册”
package com.ceshi; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.s ...