POJ2457 Part Acquisition

Part Acquisition
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4693 Accepted: 1998 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.

Source


题目大意讲的是一头牛要去获得一个牛奶机器,有n个行星和k个物品,它必须通过物物交换获得他所想要的物品,
它有物品1,机器是物品k,要通过交换获得物品k并使得次数最少。 
思路很简单,其实就是求从1到k的最短路径,直接上dijkstra求解即可,然后关键的是输出路径,通过path[]记录前驱,递归的输出到k点的最短路径 ,path[i]表示到达i点的 最短路径的前驱 。详细过程见下面代码  


#include <cstdio>
#include <cstring>
#include <cstring>
#define INF 10000005
using namespace std;

int map[1005][1005],dis[1005],path[1005],vis[1005];
int n,k;

void PrintPath(int v){
	if(v == 1){//if v等于起点则结束递归 
		printf("1\n");
		return ;
	}
	else{
		PrintPath(path[v]);//寻找前驱 
		printf("%d\n",v);
	}
}

void dijkstra(){
	for(int i = 1; i <= k; i ++){
		dis[i] = map[1][i];
		if(i != 1 && map[1][i] < INF) //记录当前能到达的点的前驱 
			path[i] = 1;
	}
	vis[1] = 1;
	
	int min,v;
	for(int i = 1; i < k; i ++){
		min = INF; v = 1;
		for(int j = 1; j <= k; j ++){
			if(!vis[j] && dis[j] < min){
				min = dis[j];
				v = j;
			}
		}
		vis[v] = 1;
		
		for(int j = 1; j <= k; j ++){
			if(!vis[j] && dis[j] > dis[v] + map[v][j]){
				dis[j] = dis[v] + map[v][j];
				path[j] = v; //记录到达j点的最短路径的前驱 
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&k);
	for(int i = 1; i <= k; i ++){
		for(int j = 1; j <= k; j ++){ //对vis[],path[],map[][]进行初始化 
			map[i][j] = INF;
		}
		vis[i] = 0;
		path[i] = -1;
	}
	
	int u,v;
	for(int i = 1; i <= n; i ++){
		scanf("%d%d",&u,&v);
		map[u][v] = 1; //因为要计算通过的顶点的个数,所以初始化1 
	}
		
	dijkstra();
		
	if(dis[k] == INF){ //判断两点间是否能够到达 
		printf("-1\n");
	}
	else{
		printf("%d\n",dis[k]+1); //输出需要经过的顶点的个数 ,顶点数是边的数目加1 
		PrintPath(k);  //递归输出最短路径 
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值