题目链接:http://poj.org/problem?id=2457
Part Acquisition
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3345 | Accepted: 1461 | 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 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.
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.
题意:奶牛身上有物品1,现在他们要换成物品k,现在给出一张有向图,i指向j表示可以用i换j,问最少要几次才可以换到需要的物品;如果换不到,输出-1。
题目读懂后就很简单,就是求出从1到k的最短路,并记录最短路的路径。
用数组模拟邻接表实现spfa,并用pre[i]记录i节点的前一个节点,在松弛的时候,如果松弛,那么就更新pre即可。
注意:这道题是有向图。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
const int mxn=50005;
int n,k;
int first[mxn],nxt[mxn<<1],vv[mxn<<1],e;
int q[mxn],head,tail,cnt,pre[mxn],dis[mxn],vis[mxn];
int ans[mxn];
void add(int u,int v)
{
vv[e]=v;
nxt[e]=first[u];
first[u]=e++;
}
void spfa(int s,int t)
{
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[s]=0;vis[s]=1;pre[s]=-1;
q[tail]=s;++cnt;tail=(tail+1)%mxn;
while (cnt!=0)
{
int u=q[head];
head=(head+1)%mxn;--cnt;vis[u]=0;
for (int i=first[u];i!=-1;i=nxt[i])
{
int v=vv[i];
if (dis[v]>dis[u]+1)
{
dis[v]=dis[u]+1;
pre[v]=u;//松弛后就更新前驱节点
if (vis[v]==0)
{
vis[v]=1;
q[tail]=v;
tail=(tail+1)%mxn;
++cnt;
}
}
}
}
}
int main()
{
while (scanf("%d%d",&n,&k)!=EOF)
{
memset(first,-1,sizeof(first));e=0;
for (int i=0;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
cnt=0;
spfa(1,k);
if (dis[k]==INF) {printf("%d\n",-1);continue;}
int x=0;
for (int i=k;i!=-1;i=pre[i])//在开个数组将它倒序存储,然后输出。
{
ans[x++]=i;
}
printf("%d\n",x);
for (int i=x-1;i>=0;--i)
{
printf("%d\n",ans[i]);
}
}
return 0;
}