POJ part acquisition
来源:互联网 发布:csv导入oracle数据库 编辑:程序博客网 时间:2024/05/22 12:14
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.
寻找最短路径,输出路径
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
int n,k;
int dis[50001],w[1001][1001],pre[50002];
bool vis[50001];
int path[1001],ans[1001];
void dijkstra(int x)
{
int i,j,Min,kk=0,next;
for(i=1;i<=k;i++)
{
dis[i]=w[x][i];
if(i!=x&&dis[i]<inf)
path[i]=x;
}
vis[x]=1;
dis[x]=0;
for(i=1;i<k;i++)
{
Min=inf;
for(j=1;j<=k;j++)
{
if(!vis[j]&&Min>dis[j])
{
Min=dis[j];
next=j;
}
}
vis[next]=1;
for(j=1;j<=k;j++)
{
if(!vis[j]&&dis[next]+w[next][j]<dis[j]&&w[next][j]!=inf)
{
dis[j]=dis[next]+w[next][j];
path[j]=next;//j下一个要放的点,next上一次循环中已经确定要放的点
//cout<<j<<" "<<path[j]<<endl;
}
}
}
}
void print()
{
int i;
memset(ans,0,sizeof(ans));
int cnt=0;
if(dis[k]==inf)
cout<<"-1"<<endl;
else
{
ans[cnt++]=k;
int temp=path[k];//下标是当前点,数组值为前一个节点
while(temp)
{
//cout<<temp<<endl<<endl;
ans[cnt++]=temp;
temp=path[temp];
//cout<<temp<<endl;
}
cout<<cnt<<endl;
for(i=cnt-1;i>=0;i--)
cout<<ans[i]<<endl;
}
}
int main()
{
int i,j,a,b;
scanf("%d%d",&n,&k);
memset(vis,0,sizeof(vis));
memset(dis,inf,sizeof(dis));
//memset(w,0x3f3f3f3f,sizeof(w));
for(i=1;i<=k;i++)
for(j=1;j<=k;j++)
w[i][j]=inf;
for(i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
w[a][b]=1;
}
dijkstra(1);
//cout<<dis[k]+1<<endl;
print();
return 0;
}
- poj 2457 Part Acquisition
- poj 2457 Part Acquisition
- POJ 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
- POJ 2457 Part Acquisition (Dijkstra + 记录路径)
- POJ 2457 Part Acquisition【Dij+记录路径】
- POJ 2457 Part Acquisition(记录路径单源最短路)
- poj——2457——Part Acquisition
- poj 2457 Part Acquisition 【spfa最短路 + STL路径输出】
- POJ 2457--Part Acquisition 【spfa最短路 + STL路径输出】
- poj 2457 Part Acquisition最短路径dijikstra迪杰斯特拉
- bzoj1674 [Usaco2005]Part Acquisition
- 1674: [Usaco2005]Part Acquisition
- LeetCode132 Palindrome Partitioning II&I
- 关于Runable、Handler的循环
- Linux下安装zookeeper以及Zookeeper的集群搭建
- Java 网络通信 — BIO通信例子
- 触发器
- POJ part acquisition
- Node.js Module – exports vs module.exports
- 表白1314
- Hdu 第几天?
- 区间dp uva 10003
- 北大百炼评测系统使用说明
- POI20143832: [Poi2014]Rally
- 存储过程
- HDU