POJ 2457 Part Acquisition (Dijkstra + 记录路径)
2015-02-10 00:38
375 查看
Part Acquisition
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
Sample Output
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
USACO 2005 February Silver
题目链接:http://poj.org/problem?id=2457
题目大意:有向图,一共有n条边,求从点1开始到点k的最短路径,不存在输出-1
题目分析:裸的Dijkstra,逆向记录下路径就好了,水题
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3767 | Accepted: 1631 | Special Judge |
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
USACO 2005 February Silver
题目链接:http://poj.org/problem?id=2457
题目大意:有向图,一共有n条边,求从点1开始到点k的最短路径,不存在输出-1
题目分析:裸的Dijkstra,逆向记录下路径就好了,水题
#include <cstdio> #include <cstring> int const INF = 0xfffffff; int const MAX = 1005; int dist[MAX], map[MAX][MAX], path[MAX], ans[MAX]; int n, k; bool vis[MAX]; void Dijkstra(int v0) { for(int i = 1; i <= k; i++) { dist[i] = map[v0][i]; vis[i] = false; if(i != v0 && dist[i] < INF) path[i] = v0; } dist[v0] = 0; vis[v0] = true; int u; for(int i = 1; i <= k; i++) { int Min = INF; for(int j = 1; j <= k; j++) { if(!vis[j] && dist[j] < Min) { u = j; Min = dist[j]; } } vis[u] = true; for(int j = 1; j <= k; j++) { if(!vis[j] && map[u][j] < INF && dist[u] + map[u][j] < dist[j]) { dist[j] = dist[u] + map[u][j]; path[j] = u; } } } } void Print() { memset(ans , 0, sizeof(ans)); int cnt = 0; if(dist[k] == INF) printf("-1\n"); else { ans[cnt++] = k; int tmp = path[k]; while(tmp) { ans[cnt++] = tmp; tmp = path[tmp]; } printf("%d\n", cnt); for(int i = cnt - 1; i >= 0; i--) printf("%d\n", ans[i]); } } int main() { int x, y; scanf("%d %d", &n, &k); memset(path , 0, sizeof(path)); for(int i = 1; i <= k; i++) { for(int j = 1; j <= k; j++) { map[i][j] = INF; if(i == j) map[i][j] = 0; } } for(int i = 0; i < n; i++) { scanf("%d %d", &x, &y); map[x][y] = 1; } Dijkstra(1); Print(); }
相关文章推荐
- POJ 2457 Part Acquisition【Dij+记录路径】
- POJ 2457 Part Acquisition(记录路径单源最短路)
- poj 1122 dijkstra+记录路径
- poj 1734 Sightseeing trip_ 最小环记录路径
- poj 2387 最短路径 **dijkstra模板
- POJ 2488 DFS+记录路径 TOJ 3520 BFS+记录路径
- 如何记录Dijkstra最短路径的过程
- 最短路记录路径——PKU 2457
- POJ 1787 Charlie's Change(多重背包+记录路径)
- poj 2387 最短路径Dijkstra
- POJ--1141--Brackets Sequence--记录DP路径
- poj_2387(最短路径Dijkstra)
- POJ 1062 昂贵的聘礼(最短路径Dijkstra+枚举)
- POJ_3268(两次dijkstra、路径翻转即可)
- POJ 2253 Frogger (求每条路径中最大值的最小值,Dijkstra变形)
- POJ 2127 Greatest Common Increasing Subsequence (最长公共上升子序列+记录路径)
- poj 1787 多重背包记录路径
- poj 1141【dp--记录路径】
- POJ 1062 昂贵的聘礼(最短路径.Dijkstra)
- HDU ACM 3986 Harry Potter and the Final Battle(邻接表实现最短路dijkstra堆优化记录路径 + 枚举最短路上每条边)