POJ 2457 Part Acquisition 最短路径

注意题目中点是从1-k,k是终点,同时也是图的大小。

用的是zju的模板。list[]链表的尾用list[i]->next==NULL来判断。点从0开始,到n-1,传进去的图的大小应该是k+1,否则....等着悲剧吧大笑,反正我wa了好多次

 

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

 

 

 

#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
#define MAXN 1050
#define inf 1000000000
typedef int elem_t;
struct edge_t{
	int from,to;
	edge_t* next;
};

void dijkstra(int n,edge_t* list[],elem_t len,int s,elem_t* min,int* pre){
	edge_t* t;
	int i,*que,f=0,r=0,p=1,l=1;
	que=new int [MAXN];
	for (i=0;i<n;i++)
		min[i]=inf;
	min[que[0]=s]=0,pre[s]=-1;
	for (;r<=f;l++,r=f+1,f=p-1)
		for (i=r;i<=f;i++)
			for (t=list[que[i]];t;t=t->next)
				if (min[t->to]==inf)
					min[que[p++]=t->to]=len*l,pre[t->to]=que[i];
}

edge_t *list[MAXN],*head[MAXN];
int pre[MAXN],min_[MAXN];
int main(){
	int n,m,k,x,y,i;
	scanf("%d%d",&n,&k);
	for (i=0;i<=1002;i++) list[i]=head[i]=NULL;
	i=0;
	while (i<n){
		i++;
		scanf("%d%d",&x,&y);
		if (head[x]==NULL) {
			head[x]=list[x]=new edge_t;
			list[x]->from=x;
			list[x]->to=y;
			list[x]->next=NULL;
		}
		else {
			list[x]->next=new edge_t;
			list[x]=list[x]->next;
			list[x]->from=x;list[x]->to=y;list[x]->next=NULL;
		}
	}
	dijkstra(k+1,head,1,1,min_,pre);
	if (min_[k]==inf) {printf("-1\n");return 0;}
	printf("%d\n",min_[k]+1);
	stack<int> ss;
	ss.push(k);
	while (pre[k]!=-1){
		k=pre[k];
		ss.push(k);
	}
	while (!ss.empty()){printf("%d\n",ss.top());ss.pop();}

	return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值