Part Acquisition
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3067 | Accepted: 1320 | 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.
仔细啊:尼玛的,现有队列构造过了r.d<d打成r.u<d尼玛WA14次,仔细啊仔细啊,仔细啊
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.
仔细啊:尼玛的,现有队列构造过了r.d<d打成r.u<d尼玛WA14次,仔细啊仔细啊,仔细啊
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxn=55000;
const int maxe=100500;
const int inf=100000000;
struct HeapNode
{
int d,u;
bool operator < (const HeapNode & r)const
{
return r.d<d;
}
};
struct NoedeEdges
{
int to;
int next;
int w;
};
NoedeEdges edges[maxe];
int dist[maxn],pre[maxn],head[maxn],ans[maxn];
bool p[maxn];
int n,k,NumEdges;
void AddEdges(int u,int v,int w)
{
NumEdges++;
edges[NumEdges].to=v;
edges[NumEdges].w=w;
edges[NumEdges].next=head[u];
head[u]=NumEdges;
}
void Dijkstra(int s,int n)
{
priority_queue<HeapNode>q;
while(!q.empty()) q.pop();
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++)
{
dist[i]=inf;
}
dist[s]=0;
q.push((HeapNode){dist[s],s});
while(!q.empty())
{
HeapNode pi=q.top();
q.pop();
int u=pi.u;
if(p[u]) continue;
p[u]=true;
for(int k=head[u];k!=-1;k=edges[k].next)
{
if(dist[edges[k].to]>pi.d+edges[k].w)
{
dist[edges[k].to]=pi.d+edges[k].w;
pre[edges[k].to]=u;
q.push((HeapNode){dist[edges[k].to],edges[k].to});
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&k)==2)
{
memset(head,-1,sizeof(head));
memset(dist,0,sizeof(dist));
memset(edges,0,sizeof(edges));
memset(pre,0,sizeof(pre));
memset(ans,0,sizeof(ans));
NumEdges=0;
while(n--)
{
int u,v;
scanf("%d%d",&u,&v);
AddEdges(u,v,1);
}
Dijkstra(1,k);
//for(int i=1;i<=k;i++)
// printf("%d ",dist[i]);
if(dist[k]!=inf)
{
printf("%d\n",dist[k]+1);
int cnt=0;
ans[cnt++]=k;
int cur=k;
while(pre[cur]!=1)
{
ans[cnt++]=pre[cur];
cur=pre[cur];
}
ans[cnt]=1;
for(int i=cnt;i>=0;i--)
{
if(i!=0) printf("%d\n",ans[i]);
else printf("%d",ans[i]);
}
}
else
printf("-1\n");
}
return 0;
}