1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 634  Solved: 310
[Submit][Status]

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 <= N <= 100,000) on a number line and the cow is at a point K (0 <= K <= 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting. * Walking: FJ can move from any point X to the points X-1 or X+1 in a single minute * Teleporting: FJ can move from any point X to the point 2*X in a single minute. If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

    农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上幽发,尽快把那只奶牛抓回来.
    他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有
两种办法移动,步行和瞬移:步行每秒种可以让约翰从z处走到x+l或x-l处;而瞬移则可让他在1秒内从x处消失,在2x处出现.然而那只逃逸的奶牛,悲剧地没有发现自己的处境多么糟糕,正站在那儿一动不动.
    那么,约翰需要多少时间抓住那只牛呢?

Input

* Line 1: Two space-separated integers: N and K

    仅有两个整数N和K.

Output

* Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

    最短的时间.

Sample Input

5 17
Farmer John starts at point 5 and the fugitive cow is at point 17.

Sample Output

4

OUTPUT DETAILS:

The fastest way for Farmer John to reach the fugitive cow is to
move along the following path: 5-10-9-18-17, which takes 4 minutes.

HINT

 

Source

题解:
构图完了SPFA。。。
代码:
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 200000+1000
#define maxm 400000
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct edge{int go,next,w;}e[*maxm];
int n,s,t,tot,q[maxn],d[maxn],head[maxn];
bool v[maxn];
void ins(int x,int y)
{
e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
}
void insert(int x,int y)
{
ins(x,y);ins(y,x);
}
void spfa()
{
for(int i=;i<=n;++i) d[i]=inf;
memset(v,,sizeof(v));
int l=,r=,x,y;q[]=s;d[s]=;
while(l!=r)
{
x=q[++l];if(l==maxn)l=;v[x]=;
for(int i=head[x];i;i=e[i].next)
if(d[x]+<d[y=e[i].go])
{
d[y]=d[x]+;
if(!v[y]){v[y]=;q[++r]=y;if(r==maxn)r=;}
}
} }
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
s=read();t=read();
if(s>=t){printf("%d\n",abs(t-s));return ;}
else n=t+abs(t-s)+;
for(int i=;i<n;i++)insert(i,i+);
for(int i=;i<=n/;i++)ins(i,i<<);
spfa();
printf("%d\n",d[t]);
return ;
}

BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛的更多相关文章

  1. BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )

    BFS... -------------------------------------------------------------------------------------------- ...

  2. 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  3. 【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1646 这一题开始想到的是dfs啊,,但是本机测样例都已经re了... 那么考虑bfs...很巧妙? ...

  4. 【题解】[Usaco2007 Open]Catch That Cow 抓住那只牛-C++

    题目DescriptionFarmer John has been informed of the location of a fugitive cow and wants to catch her ...

  5. bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】

    满脑子dp简直魔性 模拟题意bfs转移即可 #include<iostream> #include<cstdio> #include<queue> using na ...

  6. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  7. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  8. 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)

    Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...

  9. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

随机推荐

  1. Shiro Quartz之Junit測试Session管理

    Shiro的quartz主要API上提供了org.apache.shiro.session.mgt.quartz下session管理的两个类:QuartzSessionValidationJob和Qu ...

  2. Bulk Insert 高效快速插入数据

    BULK INSERT以用户指定的格式复制一个数据文件至数据库表或视图中. 语法: BULK INSERT [ [ 'database_name'.][ 'owner' ].]{ 'table_nam ...

  3. Junit使用教程(一)

    几乎所有程序员都听说过Junit的大名,但不知真正懂得运用它的人有多少,我便是其中的一个小白. 知道Junit是用来测试的,但却把“宝刀”当成了“菜刀”用.为了从此不再菜鸟,特此总结整理了下Junit ...

  4. 解决XCode 4.x SVN无法连接的问题

    XCode升级到4.X版本后,确实好用了不少.但普通都存在SVN无法连接的问题.XCode4.x Source Control功能迁移到了File - Source Control目录下,也出现了一些 ...

  5. Android(java)学习笔记222:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)

    1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity:               startActivityForResult(intent , 0): (2)在被开启的Activi ...

  6. noip 2015 运输计划 (lca+二分)

    /* 95 最后一个点T了 qian lv ji qiong 了 没学过树剖 听chx听xzc说的神奇的方法 Orz 首先求出每个计划的路径长度 这里写的倍增 然后二分答案 对于每个ans 统计> ...

  7. oracle 关于动态执行语句 execute immediate 的用法

    当在开发的应用场景中 数据库处理复杂业务逻辑里用到 SQL 语句拼接    可以用  execute immediate   来执行语 举个例子 insert into tb_temp_public( ...

  8. dede织梦后台页面及功能修改及精简操作方法

    先让我们来看看都有哪些页面控制着后台的功能和显示.下方为系统默认的后台界面图,为了便于下面的说明我对各个部分进行了一些标示.共A.B.C.D.E五个区域. 常用:A区域[顶部LOGO行]对应文件:/d ...

  9. PHP解析xml

    <?xml version="1.0" encoding="UTF-8"?> <ZIP_result> <result name= ...

  10. wpf Content数据绑定StringFormat起作用的原理和解决(转)

    1.简单示例: <Window x:Class="WpfOne.Bind.Bind6" xmlns="http://schemas.microsoft.com/wi ...