BZOJ1674: [Usaco2005]Part Acquisition

1674: [Usaco2005]Part Acquisition

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 259  Solved: 114
[Submit][Status]

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

Silver

题解:

我会说我没看题就是为了写个dijkstra+heap的模版吗?

代码:

 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 #define inf 1000000000
12 #define maxn 50000+100
13 #define maxm 2000000
14 #define eps 1e-10
15 #define ll long long
16 #define pa pair<int,int>
17 using namespace std;
18 inline int read()
19 {
20     int x=0,f=1;char ch=getchar();
21     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
22     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
23     return x*f;
24 }
25 int n,m,tot;
26 int d[1005],head[1005];
27 bool v[1005];
28 struct edge{int go,next,w;}e[50005];
29 void insert(int x,int y,int z)
30 {
31     e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
32 }
33 void dijkstra()
34 {
35     priority_queue<pa,vector<pa>,greater<pa> >q;
36     for(int i=1;i<=n;i++)d[i]=inf;
37     memset(v,0,sizeof(v));
38     d[1]=0;q.push(make_pair(0,1));
39     while(!q.empty())
40     {
41         int x=q.top().second;q.pop();
42         if(v[x])continue;v[x]=1;
43         for(int i=head[x],y;i;i=e[i].next)
44             if(d[x]+e[i].w<d[y=e[i].go])
45             {
46                 d[y]=d[x]+e[i].w;
47                 q.push(make_pair(d[y],y));
48             }
49
50     }
51 }
52 int main()
53 {
54     freopen("input.txt","r",stdin);
55     freopen("output.txt","w",stdout);
56     m=read();n=read();
57     for(int i=1;i<=m;i++)
58     {
59         int x=read(),y=read(),z=1;
60         insert(x,y,z);
61     }
62     dijkstra();
63     if(d[n]==inf)puts("-1");
64     else printf("%d",d[n]+1);
65     return 0;
66 }

BZOJ1674: [Usaco2005]Part Acquisition

时间: 2024-07-28 12:51:59

BZOJ1674: [Usaco2005]Part Acquisition的相关文章

bzoj1674: [Usaco2005]Part Acquisition 裸dijkstra

1 #include <stdio.h> 2 #include <queue> 3 #include <string.h> 4 #include <algorithm> 5 #define inf 2000000000 6 using namespace std; 7 typedef pair<long long , int > pii; 8 priority_queue <pii, vector<pii>, greater&l

[Usaco2005]Part Acquisition

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 wh

[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

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

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的标号.由于这些行星都不是十分发达.没有流通的货币,所以

bzoj1730 [Usaco2005 dec]Barn Expansion 牛棚扩张

1730: [Usaco2005 dec]Barn Expansion 牛棚扩张 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 134  Solved: 72[Submit][Status][Discuss] Description Farmer John has N (1 <= N <= 25,000) rectangular barns on his farm, all with sides parallel to the X and Y ax

bzoj1754[Usaco2005 qua]Bull Math*

bzoj1754[Usaco2005 qua]Bull Math 题意: 求两个正整数的积,每个数≤40位. 题解: 为什么C++不能支持高精度呢…… 代码: 1 a=int(raw_input()) 2 b=int(raw_input()) 3 print a*b 20160831

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路 题意: 数轴上n个互不覆盖的区间,问要用多少个长为L的线段覆盖.n≤10000 题解: 排序区间,然后从每个区间左端点开始铺木板,如果最后一块木板能够铺到下一个区间就铺,以此类推. 代码: 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #define maxn 10100 5 #define inc(i

1740: [Usaco2005 mar]Yogurt factory 奶酪工厂

1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 119  Solved: 100[Submit][Status][Discuss] Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10