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;
}

最新文章

  1. 在本地windows机器上安装SecureCRT客户端
  2. extjs之apply
  3. SQL2005语句实现行转列,列转行
  4. 移动测试会Ebay沙龙PPT
  5. css排版
  6. bzoj3319: 黑白树
  7. 脱离Xcode,程序在模拟器中无法运行
  8. BootStrap--模态框中 上传图片
  9. Metropolis Hasting算法
  10. U盘安装Ubuntu kylin版
  11. Android网络开发之Volley--Volley基本用法StringRequest(一)
  12. mysql 分析5语句的优化--索引添加删除
  13. Redis在本地测试没有问题,上传的服务器后出现错误
  14. Analysis CDI
  15. 安装elasticsearch-7.0.0(centos)
  16. JSP基础知识➣语法整理(二)
  17. javascript 伪数组和转化为标准数组
  18. 为springboot项目添加springboot-admin监控
  19. 在java中(==)的用法
  20. React Native &#39;config.h&#39; file not found 问题、 &#39;glog/logging.h&#39; file not found 问题、configure: error: C compiler cannot create executables问题解决过程记录

热门文章

  1. SpringMVC get请求中文乱码
  2. poj2481 Cows
  3. 架构设计经典案例:X窗体系统
  4. InfoQ中文站特供稿件:Rust编程语言的核心部件
  5. 关于Android中物理按键不响应的可能的一个问题。
  6. 暴力破解zip文件
  7. [办公自动化]chrome浏览器的书签在哪里存放
  8. SpringMVC_基本配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)
  9. 编程题:1. var person = &#39;{name:&quot;Lily&quot;,sex:&quot;famale&quot;,age:24,country:&quot;US&quot;}&#39;;将person转换成JSON对象并便利每个属性值。
  10. 用过滤器Filter判断用户是否登陆