Advertisement
royalsflush

Codeforces 29E AC (antigo)

Mar 23rd, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <queue>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8. #define rep(i,n) for (int i=0; i<n; i++)
  9.  
  10. #define TRACE(x)  
  11. #define PRINT(x...) TRACE(printf(x))
  12. #define WATCH(x) TRACE(cout << #x << " = " << x << endl;)
  13.  
  14. #define fi first
  15. #define se second
  16. #define mp make_pair
  17.  
  18. int n,m;
  19. int k;
  20. queue< pair< pair<int, int>, int> > q;
  21. vector<int> adj[510];
  22. int dist[510][510][2];
  23. pair<int, int> pre[510][510][2];
  24. int bpath[510];
  25. int apath[510];
  26.  
  27. void bfs() {
  28.     q.push(mp(mp(0,n-1),0));
  29.     dist[0][n-1][0]=0; 
  30.  
  31.     while (!q.empty()) {
  32.         int al=q.front().fi.fi;
  33.         int bob=q.front().fi.se;
  34.         int mv=q.front().se;
  35.         q.pop();
  36.  
  37.         if (mv) rep(i,adj[al].size()) {
  38.             int na=adj[al][i];
  39.             if (bob==na) continue;
  40.             if (dist[na][bob][0]!=-1) continue;
  41.            
  42.             dist[na][bob][0]=dist[al][bob][1]+1;
  43.             pre[na][bob][0]=mp(al,bob);
  44.             q.push(mp(mp(na,bob),0));  
  45.         }
  46.         else rep(i,adj[bob].size()) {
  47.             int nb=adj[bob][i];
  48.             if (dist[al][nb][1]!=-1) continue;
  49.             if (al==n-1 && nb==0) continue;
  50.  
  51.             dist[al][nb][1]=dist[al][bob][0]+1;
  52.             pre[al][nb][1]=mp(al,bob);
  53.             q.push(mp(mp(al,nb),1));
  54.         }
  55.     }
  56. }
  57.  
  58. int main() {
  59.     scanf("%d %d", &n,&m);
  60.     rep(i,m) {
  61.         int a,b;
  62.         scanf("%d %d", &a,&b);
  63.         adj[--a].push_back(--b);
  64.         adj[b].push_back(a);
  65.     }
  66.  
  67.     memset(dist,0xff, sizeof(dist));
  68.     bfs();
  69.  
  70.     TRACE(
  71.         rep(i,n) {
  72.             rep(j,n) printf("(%d, %d) ", dist[i][j][0], dist[i][j][1]);
  73.             printf("\n");
  74.         }
  75.     )
  76.  
  77.     if (dist[n-1][0][0]==-1) printf("-1\n");
  78.     else {
  79.         k=dist[n-1][0][0]/2;
  80.         printf("%d\n", k);
  81.         int a=n-1, b=0;    
  82.         int mv=0;
  83.  
  84.         for (int i=dist[n-1][0][0]; i>=0; i--) {
  85.             apath[i/2]=a+1; bpath[(i+1)/2]=b+1;
  86.  
  87.             int tmp=pre[a][b][mv].fi;
  88.             b=pre[a][b][mv].se;
  89.             a=tmp; mv++; mv%=2;
  90.         }
  91.  
  92.         rep(i,k+1) printf("%d ", apath[i]);
  93.         printf("\n");
  94.         rep(i,k+1) printf("%d ",bpath[i]);
  95.         printf("\n");
  96.     }
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement