# 【BZOJ1674/POJ2457/Usaco2005】Part Acquisition

### Part Acquisition

 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5339 Accepted: 2225 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.

直接上dijkstra就行了。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <queue>
using namespace std;

const int Maxn=1005;
const int Maxm=50005;
int n,m,size,tot;
int first[Maxn],dis[Maxn],pre[Maxn],ans[Maxn];
struct shu{int to,next,len;};
shu edge[Maxm<<1];

inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') f=-1,c=getchar();
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}

inline void build(int x,int y)
{
edge[++size].next=first[x];
first[x]=size;
edge[size].to=y;
}

inline void dijkstra()
{
memset(dis,0x3f3f,sizeof(dis));
priority_queue<pair<int,int> >q;
q.push(make_pair(0,1)),dis[1]=0;
while(!q.empty())
{
int point=q.top().second;
q.pop();
for(int u=first[point];u;u=edge[u].next)
{
int to=edge[u].to;
if(dis[to] > dis[point] + 1)
{
dis[to] = dis[point] + 1;
pre[to]=point;
q.push(make_pair(-dis[to],to));
}
}
}
}

int main()
{
m=get_int(),n=get_int();
for(int i=1;i<=m;i++)
{
int x=get_int(),y=get_int();
build(x,y);
}
dijkstra();
if(dis[n]==dis[0]) cout<<"-1\n";
else
{
cout<<dis[n]+1<<"\n";
int x=n;
while(1)
{
ans[++tot]=x;
x=pre[x];
if(!x) break;
}
for(int i=tot;i>=1;i--) cout<<ans[i]<<"\n";
}
return 0;
}

11-28 73

11-01 26
11-15 941
10-28 517
07-27 662
05-18 9495
10-10 572
05-06 2848
10-28 796
03-03 1010