BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易
2014-10-04 00:45
363 查看
3392: [Usaco2005 Feb]Part Acquisition 交易
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 26 Solved: 18
[Submit][Status]
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 51 3
3 2
2 3
3 1
2 5
5 4
Sample Output
4奶牛们至少要交换4次,先用1去交换3,再用3去交换2,最后用2交换得到5.
HINT
Source
Silver题解:
呵呵,又一道题意不明的题。
每行是两个数字Ai和Bi,表示第i颗行星愿意提供Ai为得到Bi.
。。。。。。
这不是说 有了 bi 可以得到 ai吗?
为此它们准备依次经过N(1≤N≤50000)颗行星
有依次?
呵呵。。。
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 1000000000 #define maxn 100000 #define maxm 100000 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--) #define mod 1000000007 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} return x*f; } struct edge{int go,next;}e[2*maxm]; int n,m,k,tot,q[maxn],d[maxn],head[maxn]; bool v[maxn]; inline void insert(int x,int y) { e[++tot].go=y;e[tot].next=head[x];head[x]=tot; } void spfa() { for(int i=1;i<=m;++i) d[i]=inf; memset(v,0,sizeof(v)); int l=0,r=1,x,y;q[1]=1;d[1]=0; while(l!=r) { x=q[++l];if(l==maxn-1)l=0;v[x]=0; for(int i=head[x];i;i=e[i].next) if(d[x]+1<d[y=e[i].go]) { d[y]=d[x]+1; if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn-1)r=0;} } } } int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();m=read(); for1(i,n) { int x=read(),y=read(); insert(x,y); } spfa(); printf("%d\n",d[m]==inf?-1:d[m]+1); return 0; }
View Code
相关文章推荐
- 【BZOJ】3392: [Usaco2005 Feb]Part Acquisition 交易(spfa)
- bzoj:3392: [Usaco2005 Feb]Part Acquisition 交易
- bzoj 3392: [Usaco2005 Feb]Part Acquisition 交易(最短路)
- bzoj 3392: [Usaco2005 Feb]Part Acquisition 交易 最短路
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】
- bzoj 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 (二分+最大流)
- 【BZOJ】1676: [Usaco2005 Feb]Feed Accounting 饲料计算(差分)
- bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算 差分
- 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流
- [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛
- 【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)
- BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
- bzoj 1676: [Usaco2005 Feb]Feed Accounting 饲料计算(差分)
- bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
- bzoj1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
- bzoj1732[usaco2005 feb]Jersey Politics 牛的政治 随机化
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
- bzoj1676: [Usaco2005 Feb]Feed Accounting 饲料计算