传送门:【POJ】2457 Part Acquisition
题目分析:水题。
代码如下:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define REP( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define CLR( a , x ) memset ( a , x , sizeof a )
const int MAXN = 1005 ;
const int MAXE = 50005 ;
const double eps = 1e-6 ;
struct Edge {
int v ;
Edge* next ;
} E[MAXE] , *H[MAXN] , *cur ;
int d[MAXN] , pre[MAXN] ;
int Q[MAXN] , head , tail ;
int n , m ;
void clear () {
cur = E ;
CLR ( H , 0 ) ;
}
void addedge ( int u , int v ) {
cur -> v = v ;
cur -> next = H[u] ;
H[u] = cur ++ ;
}
void bfs () {
CLR ( d , 0 ) ;
head = tail = 0 ;
Q[tail ++] = 1 ;
d[1] = 1 ;
pre[1] = 0 ;
while ( head != tail ) {
int u = Q[head ++] ;
travel ( e , H , u ) {
int v = e -> v ;
if ( !d[v] ) {
d[v] = d[u] + 1 ;
Q[tail ++] = v ;
pre[v] = u ;
}
}
}
}
void print ( int x ) {
if ( pre[x] ) print ( pre[x] ) ;
printf ( "%d\n" , x ) ;
}
double solve () {
int u , v ;
clear () ;
while ( m -- ) {
scanf ( "%d%d" , &u , &v ) ;
addedge ( u , v ) ;
}
bfs () ;
if ( !d[n] ) printf ( "-1\n" ) ;
else {
printf ( "%d\n" , d[n] ) ;
print ( n ) ;
}
}
int main () {
int cas = 0 ;
while ( ~scanf ( "%d%d" , &m , &n ) ) solve () ;
return 0 ;
}