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

【BZOJ】3392: [Usaco2005 Feb]Part Acquisition 交易(spfa)的更多相关文章

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

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

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

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

  3. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  4. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...

  5. [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】

    题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...

  6. BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...

  7. bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】

    正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案 #include<iostream> #include<cstdio> #include<queue&g ...

  8. bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】

    因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...

  9. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机

    Description 约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T ...

随机推荐

  1. Git和Repo管理使用

    Git和Repo管理使用简要介绍 http://blog.csdn.net/stevenhu_223/article/details/8828130 多仓库代码管理器Repo的安装,使用以及服务器搭建 ...

  2. Kubernetes1.6新特性:全面支持多颗GPU

    (一)  背景资料 GPU就是图形处理器,是Graphics Processing Unit的缩写.电脑显示器上显示的图像,在显示在显示器上之前.要经过一些列处理,这个过程有个专有的名词叫" ...

  3. HDU 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. IOS Exception 1(libc++abi.dylib: terminating with uncaught exception of type NSException)

    2014-08-05 22:18:46.455 SwiftUI[1329:40871] -[_TtC7SwiftUI14MViewControler clickMe]: unrecognized se ...

  5. 基于shiro+jwt的真正rest url权限管理,前后端分离

    代码地址如下:http://www.demodashi.com/demo/13277.html bootshiro & usthe bootshiro是基于springboot+shiro+j ...

  6. Linux常用指令总结

    概述 因为平时不是经常使用Linux系统,每次用到都需要重新温习一遍,这里对自己平时经常使用到的指令做个小结,方便后面直接查阅. 常用指令 登陆root指令 sudo su - 安装软件及卸载指令 d ...

  7. css hacks

    /***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...

  8. jqchart 使用的几点小技巧

    官网demo地址:http://www.jqchart.com/jquery/chart 简单示例: [javascript] $('#jqChart').jqChart({ title: 'jqCh ...

  9. python 2,3版本自动识别导入

     import sys if str(sys.version[0]) == "3":    from urllib.parse import quote_plus    from  ...

  10. server2008服务器IIS7 +PHP5.3出现500错误的排错方法

    Windows7 IIS 500 – 内部服务器错误解决方案 1.解决方法:打开IIS,在全局功能视图中找到“错误页”,双击进去后,看最右边的“操作”下的“编辑功能设置…”,将“错误响应”下的“详细错 ...