poj 2457
来源:互联网 发布:淘宝质量好的包包店 编辑:程序博客网 时间:2024/06/06 08:23
Part Acquisition
Time Limit: 1000MS Memory Limit: 65536KTotal 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.
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.
* 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.
* Lines 2..T+1: The ordered list of the objects that the cows possess in the sequence of trades.
Sample Input
6 51 33 22 33 12 55 4
Sample Output
41325
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 0x3f3f3fint 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();}}
0 0
- poj 2457
- poj 2457 Part Acquisition
- poj 2457 Part Acquisition
- POJ 2457 BFS
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- 机器学习(1):机器学习与数学分析
- 欢迎使用CSDN-markdown编辑器
- Hibernate命名查询
- 《Java高并发程序设计》学习 --2.6 线程优先级
- 微信小程序demo:贝思客:电商模板,购物车模板
- poj 2457
- 有趣的Java之调皮的浮点数
- MyEclipse 修改虚拟路径 按照教程找不到Web目录
- 掌握Linux下的常规操作
- 用指针来求两个数a,b的max&min值
- 【BZOJ1864】【ZJOI2006】三色二叉树(树形dp)
- [BZOJ1007][HNOI2008]水平可见直线(单调栈+计算几何)
- C++反射机制的实现
- CSS3 选择器——伪类选择器