bzoj 1674: [Usaco2005]Part Acquisition — dijkstra(堆优化)

  • 2017-03-19
  • 0
  • 0


1674: [Usaco2005]Part Acquisition

Time Limit: 5 Sec  Memory Limit: 64 MB


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.
这些牛被派去太空任务,为他们的谷仓购置一台新挤奶机。他们是通过一组包含N(1 < = N = 50000)行星,每一个交易岗位的恒星飞行。奶牛已经确定的K(1 < = K = 1000)类型的对象(编号为1 K)每个行星在集群的欲望,以及他们必须交易的产品。没有行星发展货币,所以他们在易货制度下工作:所有交易由每一方交易完全一个对象(大概是不同类型)。奶牛从地球开始与高品质的干草筒(项目1),他们希望一个新的挤奶机(项目k)。帮助他们找到最好的方法,使一系列的交易在行星的集群,以获得项目K.如果这个任务是不可能的,输出- 1。


* 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.


* 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



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.