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

想法很简单。。。将每一种看做一个点,如果i可以换成j,那么连边到j。。

费用都为1.。

然后拥有过的物品就是最短路+1.。

#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

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.

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

OUTPUT DETAILS:

The cows possess 4 objects in total: first they trade object 1 for
object 3, then object 3 for object 2, then object 2 for object 5.

HINT

Source

【BZOJ】1674: [Usaco2005]Part Acquisition(spfa)的更多相关文章

  1. 【BZOJ】2016: [Usaco2010]Chocolate Eating(二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2016 这些最大最小显然是二分. 但是二分细节挺多的...这里注意二分的区间,可以累计所有的可能,然后 ...

  2. 【BZOJ】1295: [SCOI2009]最长距离(spfa+暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1295 咳咳..此题我不会做啊..一开始认为是多源,可是有移除物品的操作,所以不行. 此题的思想很巧妙 ...

  3. 【BZOJ】1055: [HAOI2008]玩具取名(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1055 我竟然都没往dp这个方向想.....百度了下看到标题是dp马上就会转移了QAQ... 设d[i ...

  4. 【BZOJ】2820: YY的GCD(莫比乌斯)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2820 此题非常神! 下文中均默认n<m 首先根据bzoj1101的推理,我们易得对于一个数d使 ...

  5. 【BZOJ】1084: [SCOI2005]最大子矩阵(DP)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1084 有一个1A--- 本题没看懂,,不会啊囧..感觉完全设不了状态..看了题解,囧,m<=2 ...

  6. 【BZOJ】1052: [HAOI2007]覆盖问题(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1052 首先膜拜题解orz,表示只能想到二分... 贪心就是每一次找到一个最小的能包围所有点的矩阵,然 ...

  7. 【BZOJ】1046: [HAOI2007]上升序列(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1046 一直看错题....................... 这是要求位置的字典序啊QQQAAAQ ...

  8. 【BZOJ】1029: [JSOI2007]建筑抢修(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1029 按右端点排序后依次加入,并且每一次看是否能被修筑,如果能就修:否则查找原来修过的,如果原来修过 ...

  9. 【BZOJ】3297: [USACO2011 Open]forgot(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3297 这题拖了很久呢... 很久以前写了个dfs,,但是tle了..... 然后一直想dp想不出来, ...

随机推荐

  1. 算法笔记_046:跳台阶问题(Java)

    目录 1 问题描述 2 解决方案 2.1 递归法 2.2 迭代法   1 问题描述 一个台阶总共有n级,如果一次可以跳1级,也可以跳2级,求总共有多少种跳法. 2 解决方案 2.1 递归法 如果整个台 ...

  2. UML和模式应用学习笔记-2(迭代和进化式开发)

    一:什么是迭代和进化式开发 1:迭代和进化式开发:通常会在还没有详细定义所有需求的情况下假设开发开始,同时使用反馈来明确和改进演化中的规格说明: 2:迭代方法与较高的成功率.生产率和低缺陷率具有关系: ...

  3. 【Oracle】表空间相关集合

    创建表空间 1.语法 CREATE [SMALLFILE/BIGFILE] TABLESPACE tablespace_nameDATAFILE '/path/filename' SIZE num[k ...

  4. 【Linux】more命令

    用途 more主要用于一页一页查看档案 全称 全称即为more 说明 空格键 :代表向下翻一页,也可以使用CTRL+F Enter :代表下翻一行 :f      :立刻显示出文件名以及目前显示的行数 ...

  5. VS2012不能创建数据库连接出现或者连接数据库时提示:未能载入文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, C

    VS2012不能创建数据库连接时或者连接数据库时提示:未能载入文件或程序集"Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, ...

  6. Linux命令-压缩解压命令:tar

    tar [选项] [打包后文件名] [打包前的文件或者目录名称] -c表示创建(create-创建) -z表示压缩(gzip-压缩) -j表示压缩(bzip2-压缩) -v显示进度(verbose-冗 ...

  7. ZOJ 3427 Array Slicing (scanf使用)

    题意  Watashi发明了一种蛋疼(eggache) 语言  你要为这个语言实现一个 array slicing 函数  这个函数的功能是 有一个数组初始为空  每次给你一个区间[ l, r)  和 ...

  8. 【HTML5 】手机重力与方向感应的应用——摇一摇效果

    http://www.helloweba.com/view-blog-287.html HTML5有一个重要特性:DeviceOrientation,它将底层的方向和运动传感器进行了高级封装,它使我们 ...

  9. numpy 文件存取 npy、npz

    转处:http://blog.csdn.net/pipisorry/article/details/39088003 NumPy提供了多种文件操作函数方便我们存取数组内容. 文件存取的格式:二进制和文 ...

  10. unity5, animation event

    一,给导入的fbx动画添加animation event: 如下图,在双击状态机中的idle状态,打开右面的面板,点开Events项会出现一个时间轴,点击下方播放器的播放按钮或者拖动播放器时间轴上的红 ...