Advertisement
nicuvlad76

Untitled

Feb 26th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <queue>
  4. #define N 1005
  5. #define oo 0x3f3f3f3f
  6. using namespace std;
  7. using VI=vector<int>;
  8. using VII=vector<VI>;
  9.  
  10. ifstream fin("zoomba.in");
  11. ofstream fout("zoomba.out");
  12. queue<int> q;
  13.  
  14. VII g, d;
  15. VI a;
  16. int n,m,k,z,x,y;
  17. void Lee(int v)
  18. {
  19. int x;
  20. while(!q.empty())
  21. {
  22. x=q.front();
  23. q.pop();
  24. for(auto y:g[x])
  25. if(d[y][v]>d[x][v]+1)
  26. d[y][v]=d[x][v]+1, q.push(y);
  27. }
  28. }
  29. int main()
  30. {
  31. fin>>n>>m>>k>>z;
  32. a=VI(n+1);
  33. g=VII(n+1);
  34. d=VII(n+1,VI((1<<k), oo));
  35. for(int i=0;i<k;i++)
  36. fin>>a[i];
  37. for(int i=1;i<=m;i++)
  38. {
  39. fin>>x>>y;
  40. g[x].push_back(y);
  41. g[y].push_back(x);
  42. }
  43. for(int i=0;i<k;i++)
  44. {
  45. d[a[i]][1<<i]=0;
  46. q.push(a[i]);
  47. Lee(1<<i);
  48. }
  49. for(int cnt=2;cnt<=k; cnt++)
  50. for(int j=1; j<(1<<k);j++)
  51. if(__builtin_popcount(j)==cnt)
  52. {
  53. for(int i=1;i<=n;i++)
  54. {
  55. for(int p=1;p<j;++p)
  56. if((p|j)==j)
  57. d[i][j]=min(d[i][j], d[i][p]+d[i][j^p]);
  58. q.push(i);
  59. }
  60. Lee(j);
  61. }
  62. if(d[z][(1<<k)-1]==oo)fout<<-1;
  63. else fout<<d[z][(1<<k)-1];
  64. return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement