http://www.lydsy.com/JudgeOnline/problem.php?id=3392

同1674

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005, Q=N*100, oo=~0u>>2;
int q[Q], front, tail, d[N], vis[N], ihead[N], cnt, n, x;
struct ED { int to, next; }e[Q];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
}
int spfa() {
q[tail++]=1;
for1(i, 2, 1005) d[i]=oo;
vis[1]=1;
while(front!=tail) {
int v, u=q[front++]; if(front==Q) front=0; vis[u]=0;
for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+1) {
d[v]=d[u]+1;
if(!vis[v]) {
vis[v]=1;
q[tail++]=v; if(tail==Q) tail=0;
}
}
}
return d[x];
} int main() {
read(n); read(x);
for1(i, 1, n) {
int u=getint(), v=getint();
add(u, v);
}
int ans=spfa();
if(ans==oo) puts("-1");
else print(ans+1);
return 0;
}

Description

    奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物进行了由1到K的标号.由于这些行星都不是十分发达.没有流通的货币,所以在每个市场里都只能用固定的一种货物去换取另一种货物.    奶牛们带着一种上好的饲料从地球出发,希望进行最少的交易,最终得到所需要的机器.饲料的标号为1,所需要的机器的标号为K.如果任务无法完成,输出-1.

Input

    第1行是两个数字N和K.
    第2到N+1行,每行是两个数字Ai和Bi,表示第i颗行星愿意提供Ai为得到Bi.

Output

    第1行输出最小交换次数

Sample Input

6 5
1 3
3 2
2 3
3 1
2 5
5 4

Sample Output

4

奶牛们至少要交换4次,先用1去交换3,再用3去交换2,最后用2交换得到5.

HINT

Source

最新文章

  1. encodeURI来解决URL传递时的中文问题
  2. android学习笔记52——手势Gesture,增加手势、识别手势
  3. 有关CLR的初学小整理2(可能理解不深刻,望大牛指出)
  4. 洛谷P1993 小 K 的农场
  5. 【Linux】基于Linux的buffer和cache学习
  6. oracle 查看隐含参数脚本
  7. Class.forName()数据库驱动
  8. CentOS快捷键总结
  9. bundle export fail
  10. jsDOM编程-小球在盒子里来回撞击
  11. BZOJ 4569: [Scoi2016]萌萌哒 [并查集 倍增]
  12. python初识(二)
  13. [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree
  14. SQL Server 锁机制
  15. 详解promise、async和await的执行顺序
  16. Linux实际常用命令
  17. windows下网络丢包模拟软件(Network Emulator for Windows Toolkit)
  18. pta_l1-6(连续因子)
  19. c++赋值运算符为什么要返回引用?
  20. 什么是mysql的事务和实现

热门文章

  1. 常用命令(过滤、管道、重定向、ping 命令、netstat 命令、ps命令)
  2. Git库搭建好之后,当要提交一个新的文件,需要做的是3个步骤
  3. java类与对象_成员变量和局部变量区别
  4. 运用Unity实现AOP拦截器[结合异常记录实例]
  5. ES2017 keys,values,entries使用
  6. 学会Git玩转Github笔记(二)——Git使用
  7. jetty maven插件端口设置
  8. 在项目中增加自定义icon图标
  9. 【ASP.NET MVC系列】详解View
  10. Android studio使用心得(二)— 打包签名apk发布