Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int NMAX = 205;
  6. const int KMAX = 11;
  7. const int INF = (1 << 30);
  8.  
  9. struct el
  10. {
  11. int node, cost;
  12. el()
  13. {
  14. this->node = this->cost = 0;
  15. }
  16. el(int node, int cost)
  17. {
  18. this->node = node;
  19. this->cost = cost;
  20. }
  21. inline bool operator<(const el &other) const
  22. {
  23. return this->cost > other.cost;
  24. }
  25. };
  26.  
  27. int N, M, K, Z;
  28. vector <int> graph[NMAX];
  29. int dp[NMAX][(1 << KMAX)];
  30. int dist[NMAX][NMAX], d[NMAX], friends[KMAX];
  31. bitset <NMAX> seen;
  32.  
  33. void Dijkstra(int node)
  34. {
  35. seen.reset();
  36. priority_queue <el> pq;
  37. for (int i = 0;i < N;++i)
  38. d[i] = INF;
  39. d[node] = 0;
  40. pq.push(el(node, 0));
  41. while (!pq.empty())
  42. {
  43. int cnode = pq.top().node;
  44. pq.pop();
  45. if (seen[cnode] == 0)
  46. {
  47. seen[cnode] = 1;
  48. for (auto &i : graph[cnode])
  49. {
  50. if (d[i] > d[cnode] + 1)
  51. {
  52. d[i] = d[cnode] + 1;
  53. pq.push(el(i, d[i]));
  54. }
  55. }
  56. }
  57. }
  58. }
  59.  
  60. int main()
  61. {
  62. ifstream fin("zoomba.in");
  63. ofstream fout("zoomba.out");
  64. fin >> N >> M >> K >> Z;
  65. for (int i = 0;i < K;++i)
  66. fin >> friends[i];
  67. for (int i = 1;i <= M;++i)
  68. {
  69. int u, v;
  70. fin >> u >> v;
  71. graph[u].push_back(v);
  72. graph[v].push_back(u);
  73. }
  74. for (int i = 0;i < K;++i)
  75. {
  76. Dijkstra(friends[i]);
  77. for (int j = 0;j < K;++j)
  78. dist[i][j] = d[friends[j]];
  79. }
  80. for (int i = 0;i < K;++i, cout << "\n")
  81. for (int j = 0;j < K;++j, cout << " ")
  82. for (int i = 0;i < K;++i)
  83. dp[i][(1 << i)] = 0;
  84. for (int state = 0;state < (1 << K);++state)
  85. for (int i = 0;i < K;++i)
  86. if ((state & (1 << i)) > 0)
  87. for (int j = 0;j < K;++j)
  88. if ((state & (1 << j)) == 0)
  89. dp[j][state ^ (1 << j)] = min(dp[j][state ^ (1 << j)], dp[i][state] + dist[i][j]);
  90. int answer = INF;
  91. Dijkstra(Z);
  92. for (int i = 0;i < K;++i)
  93. answer = min(answer, dp[i][(1 << K) - 1] + d[i]);
  94. if (answer == INF)
  95. answer = -1;
  96. fout << answer << "\n";
  97. fin.close();
  98. fout.close();
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement