Description

The cows have been sent on a mission through space to acquire a new milking machine for their barn. They are flying through a cluster of stars containing N (1 <= N <= 50,000) planets, each with a trading post. The cows have determined which of K (1 <= K <= 1,000) types of objects (numbered 1..K) each planet in the cluster desires, and which products they have to trade. No planet has developed currency, so they work under the barter system: all trades consist of each party trading exactly one object (presumably of different types). The cows start from Earth with a canister of high quality hay (item 1), and they desire a new milking machine (item K). Help them find the best way to make a series of trades at the planets in the cluster to get item K. If this task is impossible, output -1.

有n个星球,开始你的手中有1号物品,每个星球会帮你把物品a换成物品b,问你要得到物品m最少要换几次,如果怎么样都不能换到,输出-1

Input

  • Line 1: Two space-separated integers, N and K. * Lines 2..N+1: Line i+1 contains two space-separated integers, a_i and b_i respectively, that are planet i's trading trading products. The planet will give item b_i in order to receive item a_i.

Output

  • Line 1: One more than the minimum number of trades to get the milking machine which is item K (or -1 if the cows cannot obtain item K).

Sample Input

6 5 //6个星球,希望得到5,开始时你手中有1号物品.

1 3 //1号星球,帮你把1号物品换成3号

3 2

2 3

3 1

2 5

5 4

Sample Output

4


这题裸的最短路,什么都不用想

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=5e4;
int pre[N+10],now[N+10],child[N+10];
int h[N+10],dis[N+10];
bool vis[N+10];
int tot,n,End,root=1;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
void SPFA(int x){
int head=0,tail=1;
memset(dis,63,sizeof(dis));
h[1]=x,vis[x]=1,dis[x]=1;
while (head!=tail){
if (++head>N) head=1;
int Now=h[head];
for (int p=now[Now],son=child[p];p;p=pre[p],son=child[p])
if (dis[son]>dis[Now]+1){
dis[son]=dis[Now]+1;
if (!vis[son]){
if (++tail>N) tail=1;
h[tail]=son,vis[son]=1;
}
}
vis[Now]=0;
}
}
int main(){
n=read(),End=read();
for (int i=1,x,y;i<=n;i++) x=read(),y=read(),join(x,y);
SPFA(root);
dis[End]!=inf?printf("%d\n",dis[End]):printf("-1\n");
return 0;
}

[Usaco2005]Part Acquisition的更多相关文章

  1. Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 337  Solved: 162[Sub ...

  2. BZOJ1674: [Usaco2005]Part Acquisition

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 259  Solved: 114[Sub ...

  3. bzoj 1674: [Usaco2005]Part Acquisition -- dijkstra(堆优化)

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MB Description The cows have b ...

  4. 【BZOJ】1674: [Usaco2005]Part Acquisition(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1674 想法很简单...将每一种看做一个点,如果i可以换成j,那么连边到j.. 费用都为1.. 然后拥 ...

  5. usaco silver

    大神们都在刷usaco,我也来水一水 1606: [Usaco2008 Dec]Hay For Sale 购买干草   裸背包 1607: [Usaco2008 Dec]Patting Heads 轻 ...

  6. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

    3392: [Usaco2005 Feb]Part Acquisition 交易 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 26  Solved:  ...

  9. bzoj:3392: [Usaco2005 Feb]Part Acquisition 交易

    Description     奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物 ...

随机推荐

  1. java的计时:毫秒、纳秒

    System.currentTimeMillis()获取毫秒值,但是其精度依赖操作系统 想实现较为精确的毫秒,可以采用 System.nanoTime()/1000000L System.nanoTi ...

  2. 【APUE】wait与waitpid函数

    当一个进程终止时,内核就向其父进程发送SIGCHLD信号.因为子进程终止是个异步事件,所以这种信号也是内核向父进程发的异步通知.父进程可以选择忽略该信号,或者提供一个该信号发生时即被调用执行的函数.对 ...

  3. Jekyll 搭建

    Jekyll搭建个人主页 早上昨晚上弄hexo的时候就开始有问题,发现命令有错误!今天早上来就开始把另外建立了仓库,转Jekyll,并不顺利,做了一天了,基本的theme的本地和远端主页可以显示,但当 ...

  4. vim配置为IDE环境(超详细,极力推荐 git)

    https://github.com/yangyangwithgnu/use_vim_as_ide 1. 用法 git clone https://github.com/VundleVim/Vundl ...

  5. SQL 快速参考

    SQL 快速参考 SQL 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition ALTE ...

  6. 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)

    如约而至,Java 10 正式发布!   3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...

  7. 使用RNN解决句子对匹配问题的常见网络结构

    /* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 除了序列标注问题外,句子对匹配(Sentence Pair Matching)问题也是NLP中非经常见 ...

  8. OTG识别原理

    如图1所示,那个是Micro USB的母头,可以清晰看到有5pin,分别是(不分顺序):Vbus.GND.D+.D-.ID.当移动设备,类似手机.平板电脑等要支持OTG功能,就必须使用这个接口,而且电 ...

  9. 深度学习笔记之使用Faster-Rcnn进行目标检测 (原理篇)

    不多说,直接上干货! Object Detection发展介绍 Faster rcnn是用来解决计算机视觉(CV)领域中Object Detection的问题的.经典的解决方案是使用: SS(sele ...

  10. Elasticsearch - 搜索类型与搜索位置

    一.搜索类型: Elasticsearch同意用户选择其所希望的处理查询的方式. 由于存在一些不同的情形,对其使用不同的搜索类型才是合适的.为了控制查询的运行方式,我们能够在请求中使用search_t ...