传送门

神奇的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)的更多相关文章

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

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

  2. 洛谷—— P1849 [USACO12MAR]拖拉机Tractor

    https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...

  3. [USACO12MAR]拖拉机

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

  4. USACO Tractor

    洛谷 P3073 [USACO13FEB]拖拉机Tractor 洛谷传送门 JDOJ 2351: USACO 2013 Feb Silver 2.Tractor JDOJ传送门 题意翻译 题目描述 F ...

  5. ImageNet2017文件下载

    ImageNet2017文件下载 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PASCAL ...

  6. ImageNet2017文件介绍及使用

    ImageNet2017文件介绍及使用 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PAS ...

  7. USACO 2012 March Silver Tractor /// 优先队列BFS oj21567

    题目大意: 输入n,(x,y):n为阻挡的草堆数量,(x,y)为开始时拖拉机所在的位置 接下来n行每行一个坐标(a,b):为各个草堆的坐标 输出拖拉机要回到原点(0,0)需要移动的草堆数量 Sampl ...

  8. 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa

    3627: [JLOI2014]路径规划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 186  Solved: 70[Submit][Status] ...

  9. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

随机推荐

  1. poj3280 Cheapest Palindrome

    思路: 区间dp.添加和删除本质相同. 实现: #include <iostream> #include <cstdio> using namespace std; int n ...

  2. Ajax的项目搭建

    在搭建Ajax项目之前,首先我们的安装nginx,因为Ajax是基于nginx来运行的, 1.安装nginx 和基本的语法 http://nginx.org/ 上面的nginx的官网,下载直接安装就好 ...

  3. Scala基础篇-05求值策略

    Scala的求值策略有2种: call by value call by name 如何区分? 例子: def bar(x:Int,y: => Int) = def loop(): Int=lo ...

  4. 程序员必须知道FTP命令

                                             程序员必须知道FTP命令 文件传输软件的使用格式为:FTP<FTP地址>,若连 接成功,系统将提示用户输入 ...

  5. java实现排序的几种方法

    package com.ywx.count; import java.util.Scanner; /** * 题目:排序的几种方式(汇总及重构) * @author Vashon(yangwenxue ...

  6. MFC技术积累——基于MFC对话框类的那些事儿5

    4. 菜单 4.1 弹出菜单 本节主要讲解如何在主对话框的指定区域内通过鼠标右击来弹出一个菜单选项.最终效果图如图4.1. 如图4.1鼠标只能在指定区域(图中深色区域)内右击时弹出菜单,在指定区域外点 ...

  7. SQLite_安装

    SQLite -安装 zero-configuration SQLite闻名的特性,这意味着不需要复杂的设置或管理.本章将带你通过设置SQLite的过程在Windows.Linux和Mac OS X. ...

  8. Java 编程下 Eclipse/myeclipse 如何设置单行代码显示的最大宽度

    http://www.cnblogs.com/sunzn/archive/2013/03/30/2990191.html 或 http://zhidao.baidu.com/link?url=67uy ...

  9. Java笔记:编写第一个Java程序

    2017.6.17 1.编写第一个Java程序 创建text文本,命名第一个Java程序.txt 在里面编写Java代码 public class Demo1{ public static void ...

  10. Floyd算法简单实现(C++)

    图的最短路径问题主要包括三种算法: (1)Dijkstra (没有负权边的单源最短路径) (2)Floyed (多源最短路径) (3)Bellman (含有负权边的单源最短路径) 本文主要讲使用C++ ...