Advertisement
a53

zoomba

a53
Feb 8th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. // Popa Bogdan Ioan, clasa a 10-a, Colegiul National Aurel Vlaicu Orastie
  2. #include <bits/stdc++.h>
  3. #define kmax 10
  4. #define nmax 203
  5. #define pb push_back
  6. #define inf (int)1e9
  7. using namespace std;
  8. ifstream fin("zoomba.in");
  9. ofstream fout("zoomba.out");
  10. vector<int>g[nmax];
  11. int n,k,m,z;
  12. int i,j,l,c;
  13. int dp[nmax][1<<(kmax+1)];
  14. int a[kmax];
  15. int sol=inf;
  16. queue < int > coada;
  17. void lee(int v)
  18. {
  19. int i,k;
  20. while(!coada.empty())
  21. {
  22. k=coada.front();
  23. coada.pop();
  24. for(i=0;i<g[k].size();i++)
  25. if(dp[g[k][i]][v]>dp[k][v]+1)
  26. {
  27. dp[g[k][i]][v]=dp[k][v]+1;
  28. coada.push(g[k][i]);
  29. }
  30. }
  31. }
  32. int construct(int l)
  33. {
  34. int i=0;
  35. int ret=0;
  36. while(l--)
  37. ret^=(1<<(i++));
  38. return ret;
  39. }
  40. int nbiti(int x)
  41. {
  42. int k=0;
  43. while(x)
  44. {
  45. k+=(x&1);
  46. x>>=1;
  47. }
  48. return k;
  49. }
  50. int main()
  51. {
  52. fin>>n>>m>>k>>z;
  53. for(i=0;i<k;i++)
  54. fin>>a[i];
  55. while(m--)
  56. {
  57. fin>>i>>j;
  58. g[i].pb(j);
  59. g[j].pb(i);
  60. }
  61. for(i=1;i<=n;i++)
  62. for(j=1;j<(1<<kmax);j++)
  63. dp[i][j]=inf;
  64. for(i=0;i<k;i++)
  65. {
  66. dp[a[i]][1<<i]=0;
  67. coada.push(a[i]);
  68. lee(1<<i);
  69. }
  70. for(l=2;l<=k;l++)
  71. for(j=construct(l);j<(1<<k);j++)
  72. if(nbiti(j)==l)
  73. {
  74. for(i=1;i<=n;i++)
  75. {
  76. for(c=1;c<j;c++)
  77. if((j|c)==j)
  78. dp[i][j]=min(dp[i][j],dp[i][c]+dp[i][j^c]);
  79. coada.push(i);
  80. }
  81. lee(j);
  82. }
  83. fout<<(dp[z][(1<<k)-1] == inf ? -1 : dp[z][(1<<k)-1])<<"\n";
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement