【BZOJ1674/POJ2457/Usaco2005】Part Acquisition

56 篇文章 0 订阅
24 篇文章 0 订阅

                                                       Part Acquisition

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5339 Accepted: 2225 Special Judge

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

* Lines 2..T+1: The ordered list of the objects that the cows possess in the sequence of trades.

Sample Input

6 5
1 3
3 2
2 3
3 1
2 5
5 4

Sample Output

4
1
3
2
5

Hint

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.

 

解析:

       直接上dijkstra就行了。。。

 

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <queue>
using namespace std;

const int Maxn=1005;
const int Maxm=50005;
int n,m,size,tot;
int first[Maxn],dis[Maxn],pre[Maxn],ans[Maxn];
struct shu{int to,next,len;};
shu edge[Maxm<<1];

inline int get_int()
{
	int x=0,f=1;
	char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') f=-1,c=getchar();
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}

inline void build(int x,int y)
{
	edge[++size].next=first[x];
	first[x]=size;
	edge[size].to=y;
}

inline void dijkstra()
{
	memset(dis,0x3f3f,sizeof(dis));
	priority_queue<pair<int,int> >q;
	q.push(make_pair(0,1)),dis[1]=0;
	while(!q.empty())
	{
	  int point=q.top().second;
	  q.pop();
	  for(int u=first[point];u;u=edge[u].next)
	  {
	  	int to=edge[u].to;
	  	if(dis[to] > dis[point] + 1)
	  	{
	  	  dis[to] = dis[point] + 1;
	  	  pre[to]=point;
	  	  q.push(make_pair(-dis[to],to));
	    }
	  }
	}
}

int main()
{
	m=get_int(),n=get_int();
	for(int i=1;i<=m;i++)
	{
	  int x=get_int(),y=get_int();
	  build(x,y);
	}
	dijkstra();
	if(dis[n]==dis[0]) cout<<"-1\n";
	else
	{
	  cout<<dis[n]+1<<"\n";
	  int x=n;
	  while(1)
	  {
	  	ans[++tot]=x;
		x=pre[x];
	  	if(!x) break;
	  }
	  for(int i=tot;i>=1;i--) cout<<ans[i]<<"\n";
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值