POJ 2457--Part Acquisition 【spfa最短路 + STL路径输出】

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

题意:cows 想用自己手上的物品(1)通过多次交换得到想要的物品(k),给出两种物品的交换关系,求出至少有多少种商品经过交换,输出交换的顺序。
思路:最短路。相当于求从1商品到k商品的最短路径,不过转化过程的物品数需要算上起始的商品,中间再记录一下结点信息。路径输出用pre[]数组记录前一物品,从终点开始压栈,直到1结束,然后输出栈元素直至栈空。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> 
#include <stack>
#define maxn 1000100
#define INF 0x3f3f3f3f

using namespace std;

struct node {
	int u, v, w, next;
};

int n, k, cnt;

node edge[maxn];
int head[maxn];
int dist[maxn], pre[maxn];
bool vis[maxn];

void init(){
	cnt = 0;
	memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
	edge[cnt] = {u, v, w, head[u]};
	head[u] = cnt++;
}
void putpath(){
	stack<int>path;
	int now  = k;
	while(1){
		path.push(now);
		if(now == 1)
		break;
		now = pre[now]; 
	}
	while(!path.empty()){
		now = path.top();
		path.pop();
		printf("%d\n",now);
	}
}

void SPFA(){
	for(int i = 1; i <= n; ++i){
		dist[i] = INF;
		vis[i] = 0;
	} 
	dist[1] = 1;//注意:起点权值为1
	vis[1] = 0;
	queue<int>q;
	q.push(1);
	while(!q.empty()){
		int u = q.front();
		q.pop();
		vis[u] = 0;
		for(int i = head[u]; i != -1; i = edge[i].next){
			int v = edge[i].v;
			int w = edge[i].w;
			if(dist[v] > dist[u] + w){
				dist[v] = dist[u] + w;
				pre[v] = u;
				if(!vis[v]){
					vis[v] = 1;
					q.push(v);
			}
		}
		}
	}
	if(dist[k] != INF){
		printf("%d\n", dist[k]);
		putpath();
	}
	else 
		printf("-1\n");
}


int main (){
	
	while(scanf("%d%d", &n, &k)!=EOF){
		init();
		for(int i = 0; i < n; ++i){
			int u, v;
			scanf("%d%d", &u, &v);
			add(u, v, 1);
		}
		SPFA(); 
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值