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 5
1 3
3 2
2 3
3 1
2 5
5 4
Sample Output
4
1
3
2
5
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int M=1005;
const int inf=10000000;
int map[M][M];
int dis[M];
int flag[M];
int path[M];
int n,k;
void dijkstra()
{
int i,j;
int temp;
int u;
for (i=1;i<=k;i++)
{
dis[i]=map[1][i];
flag[i]=0;
path[i]=i;
}
flag[i]=1;
for(i=2;i<=k;i++)
{
temp=inf;
for(j=1;j<=k;j++)
{
if(!flag[j]&&dis[j]<temp)
{
temp=dis[j];
u=j;
}
}
flag[u]=1;
for(j=1;j<=k;j++)
{
if(!flag[j]&&dis[j]>dis[u]+map[u][j])
{
dis[j]=dis[u]+map[u][j];
path[j]=u;
}
}
}
}
int f()
{
int ans[M];
ans[0]=k;
int x=k;
int i=1;
int j;
while(path[x]!=x)
{
ans[i++]=path[x];
x=path[x];
}
cout<<i+1<<endl;
ans[i]=1;
for(j=i;j>=0;j--)
{
cout<<ans[j]<<endl;
}
}
int main()
{
while (cin>>n>>k)
{
for (int i=0;i<k;i++)
{
for(int j=0;j<=k;j++)
map[i][j]=inf;
}
for(int i=0;i<n;i++)
{
int a,b;
cin>>a>>b;
map[a][b]=1;
}
dijkstra();
if(dis[k]>=inf)
cout<<"-1"<<endl;
else
f();
}
return 0;
}