BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

3392: [Usaco2005 Feb]Part Acquisition 交易

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 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 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

Silver

题解:

呵呵,又一道题意不明的题。

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

。。。。。。

这不是说 有了 bi 可以得到 ai吗?

为此它们准备依次经过N(1≤N≤50000)颗行星

有依次?

呵呵。。。

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 100000
14 #define maxm 100000
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
28     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
29     return x*f;
30 }
31 struct edge{int go,next;}e[2*maxm];
32 int n,m,k,tot,q[maxn],d[maxn],head[maxn];
33 bool v[maxn];
34 inline void insert(int x,int y)
35 {
36     e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
37 }
38 void spfa()
39 {
40     for(int i=1;i<=m;++i) d[i]=inf;
41     memset(v,0,sizeof(v));
42     int l=0,r=1,x,y;q[1]=1;d[1]=0;
43     while(l!=r)
44     {
45         x=q[++l];if(l==maxn-1)l=0;v[x]=0;
46         for(int i=head[x];i;i=e[i].next)
47          if(d[x]+1<d[y=e[i].go])
48          {
49              d[y]=d[x]+1;
50              if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn-1)r=0;}
51          }
52     }
53 }
54 int main()
55 {
56     freopen("input.txt","r",stdin);
57     freopen("output.txt","w",stdout);
58     n=read();m=read();
59     for1(i,n)
60      {
61       int x=read(),y=read();
62       insert(x,y);
63      }
64     spfa();
65     printf("%d\n",d[m]==inf?-1:d[m]+1);
66     return 0;
67 }

时间: 2025-02-01 09:16:28

BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易的相关文章

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

最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i ) #defin

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第一弹)

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 196  Solved: 116[Submit][Status][Discuss] Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which hold

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

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C &l

bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算*

bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算 题意: 知道草料到来时F1kg,第D天F2kg.同时知道每头牛到来时间和离开时间,一牛一天吃1kg草料,问草料到来是第几天. 题解: 直接用区间左端点对应数组元素++,右端点+1对应数组元素--的方法,最后扫一下即可. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define inc(i

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

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: 175[Submit][Status][Discuss] Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a st

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第二弹)

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 198  Solved: 118[Submit][Status][Discuss] Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which hold

[bzoj1733][Usaco2005 feb]Secret Milking Machine 神秘的挤奶机_网络流

[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 题目大意:约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T≤200)次.他的农场里有秘密的地道,但约翰只在返回的时候用它.农场被划分成N(2≤N≤200)块区域,用1到200标号.这些区域被P(1≤P≤40000)条道路连接,每条路有一个小于10^6的长度L.两块区域之间可能有多条

[Usaco2005][BZOJ1674] Part Acquisition|dijkstra|priority_queue

1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 308  Solved: 143[Submit][Status][Discuss] Description The cows have been sent on a mission through space to acquire a new milking machine for their barn. They are flying

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

题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MaxN = 200 + 5,