poj 2457

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.


題解:牛從第一件物品,通過交易來換取第k件物品,問最短的交換方式,典型的dijkstra的題,只不過多加了存儲路徑,

用path數組來存儲路徑,每次記錄節點的後繼,最後用path2來存儲整條路徑,最後輸出。

代碼:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define INF 0x3f3f3f
int map[1007][1007], path[1007], distance[1007], visited[1007], path2[1007];
int n, k;
void dijkstra() {
	int i, j, min = INF, flag;
	for(i = 1; i <= k; i++)
		distance[i] = map[1][i];
	visited[1] = 1;
	for(i = 1; i < k; i++) {
		min = INF;
		for(j = 1; j <= k; j++)
			if(min > distance[j] && !visited[j]) {
				min = distance[j];
				flag = j;
			}
		visited[flag] = 1;
		for(j = 1; j <= k; j++)
			if(distance[j] > min + map[flag][j] && !visited[j]) {
				distance[j] = min + map[flag][j];
				path[j] = flag;
			}
	}	
}

void outPut() {
	int i, count = 0, temp;
	memset(path2, 0, sizeof(path2));
	if(distance[k] == INF)
		printf("-1\n");
	else {
		temp = path[k];
		path2[count++] = k;
		while(temp != -1) {
			path2[count++] = temp;
			temp = path[temp];
		}
		printf("%d\n", count + 1);
		printf("1\n");
		for(i = count - 1; i >= 0; i--)
			printf("%d\n", path2[i]);
	}
}

int main() {
	int i, j, start, end;
	while(scanf("%d%d", &n, &k) != EOF) {
		for(i = 1; i < 1007; i++)
			for(j = 1; j < 1007; j++)
				map[i][j] = INF;
		memset(visited, 0, sizeof(visited));
		memset(path, -1, sizeof(path));
		for(i = 1; i <= n; i++) {
			scanf("%d%d", &start, &end);
			map[start][end] = 1;
		}
		dijkstra();
		outPut();
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章