[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 ...
随机推荐
- Spring------自动化装配Bean(二)
上一篇是基于 @ComponentScan自动装配Bean的实现,这一篇将通过java手动装配bean来实现. 手动装配相对于自动装配的优点: 可以自行定义Bean的各个属性. 添加额外的方法调度. ...
- C# winform 创建快捷方式
using System;using IWshRuntimeLibrary;using System.IO; namespace UavSystem.Common{ public class S ...
- Activity的创建、生命周期
Activity是Android四大组件之一.一个Activity负责管理一个界面. 创建一个Activity: New -> Activity -> 选择要创建的Activity类型(一 ...
- 搭建SSM框架(聚合项目)
parents 父工程 pom base用户权限 jar wms业务 jar app帮助管理 war1. parents的pom.xml文件 1.1 maven servlet3.1.0 1.2 ...
- Sublime折腾记录
本文可以理解为FAQ,主要是为了大家GET一些技能,具体内容包括LICENSE.重置.Package Control的安装,其他内容以后可能补充... 最后说明一下自己的版本:Build 3114 L ...
- fedora kde桌面系统配置
本文向大家分享个人将fedora操作系统作为工作生活首选桌面系统的一些配置经验,系统版本与fedora最新版本保持一致,当前为fedora 25. #添加rpm源su -c 'dnf install ...
- KendoUI Grid Pager部分 Nan-Nan of x Items
相关问题: http://stackoverflow.com/questions/23941065/pager-error-in-kendo-gridnan-nan-of-1-items http:/ ...
- 更新github上的代码
昨晚熬夜写完了"git上传本地项目代码到github"的任务,早上来公司先把早上的工作完成后,抽点时间继续来继续更新文章 更新github上的代码 一.克隆代码 1.把大神的代码c ...
- (译文)IOS block编程指南 3 概念总览
Conceptual Overview(概览) Block objects provide a way for you to create an ad hoc function body as an ...
- Linux目录结构及详细介绍
/:根目录,位于Linux文件系统目录结构的顶层,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中. /bin,/usr/bin:该 ...