POJ2457 Part Acquisition
来源:互联网 发布:软件工程技术的应用 编辑:程序博客网 时间:2024/06/03 23:39
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.
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
题目大意讲的是一头牛要去获得一个牛奶机器,有n个行星和k个物品,它必须通过物物交换获得他所想要的物品,
它有物品1,机器是物品k,要通过交换获得物品k并使得次数最少。
思路很简单,其实就是求从1到k的最短路径,直接上dijkstra求解即可,然后关键的是输出路径,通过path[]记录前驱,递归的输出到k点的最短路径 ,path[i]表示到达i点的最短路径的前驱 。详细过程见下面代码
#include <cstdio>#include <cstring>#include <cstring>#define INF 10000005using 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 0
- POJ2457 Part Acquisition
- POJ2457 Part Acquisition
- poj2457 Part Acquisition
- POJ2457 Part Acquisition(Spfa最短路+记录路径)
- poj2457 Part Acquisition(dijkstra||spfa+路径记录优化)
- poj2457 - Part Acquisition (最短路径问题)(邻接表 Dijkstra + 并查集 )
- poj 2457 Part Acquisition
- bzoj1674 [Usaco2005]Part Acquisition
- poj 2457 Part Acquisition
- 1674: [Usaco2005]Part Acquisition
- POJ part acquisition
- [Usaco2005]Part Acquisition
- poj 2457 Part Acquisition-dijkstra
- 【POJ】2457 Part Acquisition bfs
- POJ Part Acquisition 2457 dijkstra
- POJ 2457 Part Acquisition(最短路)
- POJ 2457 Part Acquisition 最短路径
- poj 2457 Part Acquisition 最短路spfa
- JAVA—Super和this关键字
- 基于How To Tango With Django 1.7的实践(5)——Models and Databases
- Android Things做一个简易相机
- 字符串的排列
- 从达标到卓越 —— API 设计之道
- POJ2457 Part Acquisition
- Java概述
- 低电压锁定(UVLO)
- POJ 1030 Delta-wave
- js中单竖杠(|) 的作用
- Unity在Scene中自定义3D ICONS
- 搭建java环境
- finally中使用return会吃掉catch中抛出的异常
- 简单聊聊TestNG中的并发