Part Acquisition POJ - 2457 (Spfa+优先队列优化+输出路径)

这篇博客介绍了ACM竞赛中的一道问题,即如何在星际旅行中通过物物交换从地球获取新的挤奶机。问题转化为寻找最短路径的图论问题,使用SPFA(Shortest Path Faster Algorithm)配合优先队列优化来解决。如果无法获取目标物品,输出-1。博客提供了样例输入和输出,并解释了解题思路,将商品和交易方式抽象为节点和边,通过求解最短路径来确定最小交易次数和路径。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值