POJ 2457 Part Acquisition

Part Acquisition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5320   Accepted: 2217   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
带输出路径的最短路,再开一个数组记录一下就好辣
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
struct pap
{
    int from;
    int to;
    int w;
    int next;
}node[100005];
int n,m,a,b,c,t,k;
int head[100005],dis[100005];
bool vis[100005];
int num[10004];
int sum[10005];
void add(int from,int to,int w)
{
    node[t].from=from;
    node[t].to=to;
    node[t].w=w;
    node[t].next=head[from];
    head[from]=t++;
}
void spfa(int s)
{
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
        dis[i]=1000005;
    }
    dis[s]=0;
    q.push(s);
    num[++k]=s;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=node[i].next)
        {
            int v=node[i].to;
            if(dis[v]>dis[u]+node[i].w)
            {
                num[v]=u;
                dis[v]=dis[u]+node[i].w;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&m,&n))
    {
        t=1;
        k=0;
        memset(vis,false,sizeof(vis));
        memset(head,-1,sizeof(head));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b,1);
        }
        spfa(1);
        if(dis[n]==1000005)
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n",dis[n]+1);
        int ans=n;
        int ma=0;
        sum[++ma]=n;
        while(ans!=1)
        {
            sum[++ma]=num[ans];
            ans=num[ans];
        }
        for(int i=ma;i>=1;i--)
        {
            printf("%d\n",sum[i]);
        }

    }
}
View Code

 

posted @ 2018-07-31 21:28  NBLX_QAQ  阅读(120)  评论(0编辑  收藏  举报