问题描述
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.
输入
* 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.
输出
* 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.
样例输入
6 5 1 3 3 2 2 3 3 1 2 5 5 4
样例输出
4 1 3 2 5
套用SPFA模板+路径保存.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int pre[50001],dis[50001],vis[50001];int a[50001];int c[50001];
const int INF=99999999;
int n,k,top;
vector<int>vec[50001];
int spfa()
{
queue<int> que;
for(int i=1;i<=n;i++)
{
dis[i]=INF;
vis[i]=0;
pre[i]=1;
c[i]=0;
}
que.push(1);
vis[1]=1;dis[1]=0;
while(!que.empty())
{
int x=que.front();
que.pop();
vis[x]=0;
for(int i=0;i<vec[x].size();i++)
{
int y=vec[x][i];
if(dis[x]+1<dis[y])
{
dis[y]=dis[x]+1;
pre[y]=x;
if(!vis[y])
{
vis[y]=1;
c[y]++;
que.push(y);
if(c[y]>n)
return -1;
}
}
}
}
}
void add(int u,int v)
{
vec[u].push_back(v);
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF)
{
top=0;int L=k;
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
add(a,b);
}
if(spfa()!=-1&&dis[k]!=INF)
{
int ans=dis[k]+1;
printf("%d\n",ans);
int kk=0;
while(k!=1){
a[++kk]=pre[k];
k=pre[k];
}
while(kk>0)
printf("%d\n",a[kk--]);
printf("%d\n",L);
}
else
printf("-1\n");
}
}