【BZOJ】1674: [Usaco2005]Part Acquisition(spfa)
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
Input
Output
Sample Input
1 3 //1号星球,希望得到1号货物,将给你3号货物
3 2
2 3
3 1
2 5
5 4
Sample Output
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)的更多相关文章
- 【BZOJ】2016: [Usaco2010]Chocolate Eating(二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=2016 这些最大最小显然是二分. 但是二分细节挺多的...这里注意二分的区间,可以累计所有的可能,然后 ...
- 【BZOJ】1295: [SCOI2009]最长距离(spfa+暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1295 咳咳..此题我不会做啊..一开始认为是多源,可是有移除物品的操作,所以不行. 此题的思想很巧妙 ...
- 【BZOJ】1055: [HAOI2008]玩具取名(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1055 我竟然都没往dp这个方向想.....百度了下看到标题是dp马上就会转移了QAQ... 设d[i ...
- 【BZOJ】2820: YY的GCD(莫比乌斯)
http://www.lydsy.com/JudgeOnline/problem.php?id=2820 此题非常神! 下文中均默认n<m 首先根据bzoj1101的推理,我们易得对于一个数d使 ...
- 【BZOJ】1084: [SCOI2005]最大子矩阵(DP)
http://www.lydsy.com/JudgeOnline/problem.php?id=1084 有一个1A--- 本题没看懂,,不会啊囧..感觉完全设不了状态..看了题解,囧,m<=2 ...
- 【BZOJ】1052: [HAOI2007]覆盖问题(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1052 首先膜拜题解orz,表示只能想到二分... 贪心就是每一次找到一个最小的能包围所有点的矩阵,然 ...
- 【BZOJ】1046: [HAOI2007]上升序列(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1046 一直看错题....................... 这是要求位置的字典序啊QQQAAAQ ...
- 【BZOJ】1029: [JSOI2007]建筑抢修(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1029 按右端点排序后依次加入,并且每一次看是否能被修筑,如果能就修:否则查找原来修过的,如果原来修过 ...
- 【BZOJ】3297: [USACO2011 Open]forgot(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=3297 这题拖了很久呢... 很久以前写了个dfs,,但是tle了..... 然后一直想dp想不出来, ...
随机推荐
- 算法笔记_036:预排序(Java)
目录 1 问题描述 2 解决方案 2.1 检验数组中元素的唯一性 2.2 模式计算 1 问题描述 在计算机科学中,预排序是一种很古老的思想.实际上,对于排序算法的兴趣很大程度上是因为这样一个事实: ...
- wap、app移动端页面常用html标签汇总
1.section 将内容组织到精确的语义块,表示页面的一部分. 2.article article表示网页的一个文章.故事. 3.header (1)用在整页的页头 (2)section或者arti ...
- UML回想-通信图
我们对软件project这一大块的学习事实上開始的还是挺早的,而且在后来的学习过程中也不断的涉及到了这些知识. 可是,经过软考的检验来看我对软工这一块的内容掌握的实在是慘不忍睹.基本上就是一出 ...
- error: command 'gcc' failed with exit status 1 while installing eventlet
Ubuntu安装Python包出错解决办法 : sudo apt-get install python-dev sudo apt-get install libevent-dev
- Hadoop + HBase (自带zookeeper 也可单独加) 集群部署
Hadoop+HBase搭建云存储总结 PDF http://www.linuxidc.com/Linux/2013-05/83844.htm HBase 结点之间时间不一致造成regionserve ...
- hdu 4742 Pinball Game 3D(三维LIS&cdq分治&BIT维护最值)
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- websphere设置jvm参数
http://www.quiee.com.cn/archives/592/ websphere 选择服务器-> 应用程序服务器-> Server1-> 进程定义->Java 虚 ...
- android 上下左右手势判断 根据别人的改的
GestureUtils.java package com.gesture; import android.content.Context;import android.util.DisplayMet ...
- Atitit.注解解析(1)---------词法分析 attilax总结 java .net
Atitit.注解解析(1)---------词法分析 attilax总结 java .net 1. 应用场景:::因为要使用ui化的注解 1 2. 流程如下::: 词法分析(生成token流) & ...