Guest User

Untitled

a guest
Apr 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <set>
  6. using namespace std;
  7.  
  8. const int MAXN = 1000002;
  9. const int INF = 1000000000;
  10.  
  11. int D[MAXN];
  12. int a[MAXN];
  13. vector < int > G[MAXN];
  14. int n,m,tm,u,i,k,v,v1,v2,nt,wait;
  15.  
  16.  
  17.  
  18. class Mycmp
  19. {
  20. public:
  21.     bool operator () ( int v1, int v2)
  22.     {
  23.         if (D[v1] < D[v2])
  24.             return true;
  25.         if (D[v2] < D[v1])
  26.             return false;
  27.         if (D[v1] == D[v2])
  28.             return v1>v2;
  29.     }
  30. };
  31. set <int,Mycmp > X;
  32.  
  33.  
  34. int main()
  35. {
  36.     freopen("badsanta.in","r",stdin);
  37.     freopen("badsanta.out","w",stdout);
  38.     cin >> n >> m >>k;
  39.     for (i=0; i<m; i++)
  40.     {
  41.         cin >> v1 >> v2;
  42.         G[v1].push_back(v2);
  43.         G[v2].push_back(v1);
  44.     }
  45.     for (i=0; i<k; i++)
  46.         cin >> a[i];
  47.     if (a[0] == 1)
  48.     {
  49.         cout<< -1<<endl;
  50.         return 0;
  51.     }
  52.  
  53.  
  54.     for (i=1; i<=n; i++)
  55.         D[i] = INF;
  56.     D[1] = 0;
  57.     X.insert(1);
  58.     while (X.size() >0)
  59.     {
  60.         v = *X.begin();
  61.         X.erase(X.begin());
  62.         if (D[v] == INF)
  63.             break;
  64.  
  65.         for (i=0; i < G[v].size(); i++)
  66.         {
  67.             u = G[v][i];
  68.             tm = D[v];
  69.             /*if (make_pair(a [tm%k]  ,  a[(tm+1) % k]) == make_pair(u,v))
  70.                 continue;*/
  71.        
  72.             for (wait = 0; wait <= 20; wait ++)
  73.             {
  74.                 nt = tm + wait;
  75.                 if (a[nt %k ] == v)
  76.                     break;
  77.                 if (a[(tm+wait+1)%k] == v && u == a[(tm+wait) % k])
  78.                     continue;
  79.                 if (u == a[(tm+wait+1) %k])
  80.                     continue;
  81.                 if (D[u] > tm +wait +1)
  82.                 {
  83.                     X.erase(u);
  84.                     D[u] = tm + wait + 1;
  85.                     X.insert(u);
  86.                     //break;
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     if (D[n] == INF)
  92.         cout << -1 <<endl;
  93.     else
  94.         cout <<D[n] <<endl;
  95.     return 0;
  96. }
Add Comment
Please, Sign In to add comment