Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 2005
  3. #define mmax 10005
  4. using namespace std;
  5.  
  6. ifstream fin ("ubuntzei.in");
  7. ofstream fout ("ubuntzei.out");
  8.  
  9. const int pmax = (1 << 17) + 2;
  10. const int oo=(2e9);
  11. int C[21], cost[21][21], dist[nmax], dp[pmax][21], inq[nmax];
  12. priority_queue <pair <int, int> >q;
  13. vector <pair <int, int> > L[nmax];
  14. int n, m ,k;
  15.  
  16. void Read()
  17. {
  18. int x, y, c;
  19. fin >> n >> m >> k;
  20. for (int i = 1; i <= k; i++)
  21. fin >> C[i];
  22. C[0] = 1;
  23. C[++k] = n;
  24. for (int i = 1; i <= m; i++)
  25. {
  26. fin >> x >> y >> c;
  27. L[x].push_back({y,c});
  28. L[y].push_back({x,c});
  29. }
  30. }
  31.  
  32. void Dijkstra(int x)
  33. {
  34. int nod, cNode, cCost;
  35. dist[x] = 0;
  36. q.push({0, x});
  37. while(!q.empty())
  38. {
  39. nod = q.top().second;
  40. q.pop();
  41. inq[nod] = 0;
  42. for (auto i : L[nod])
  43. {
  44. cNode = i.first;
  45. cCost = i.second;
  46. if(dist[cNode] > dist[nod] + cCost)
  47. {
  48. dist[cNode] = dist[nod] + cCost;
  49. q.push({-dist[cNode], cNode});
  50. }
  51. }
  52. }
  53. }
  54.  
  55. void Initializare()
  56. {
  57. for(int i=1; i<=n; i++)
  58. dist[i] = oo;
  59. }
  60. void Solve()
  61. {
  62. for (int i = 0; i <= k; i++)
  63. {
  64. Initializare();
  65. Dijkstra(C[i]);
  66. for (int j = 0; j <= k; j++)
  67. cost[i][j] = dist[C[j]];
  68. }
  69. int mStare = (1 << k);
  70. for (int stare = 1; stare < mStare; stare++)
  71. for (int node = 0; node <= k; node++)
  72. dp[stare][node] = oo;
  73.  
  74. for (int node = 0 ; node <= k; node++)
  75. dp[(1 << node)][node] = cost[0][node];
  76.  
  77. for (int stare = 1; stare < mStare; stare++)
  78. for (int fNode = 0; fNode <= k; fNode++)
  79. if (stare & (1 << fNode))
  80. for (int sNode = 0; sNode <= k; sNode++)
  81. if (!(stare & (1 << sNode)))
  82. dp[stare | (1 << sNode)][sNode] = min(dp[stare | (1 << sNode)][sNode], dp[stare][fNode] + cost[fNode][sNode]);
  83. int mn = oo;
  84. for (int i = 0 ; i < k; i++)
  85. {
  86. ///cout << mn << " " << dp[mStare - 1][i] << " " << cost[i][k] << "\n";
  87. mn = min(mn, dp[mStare - 1][i] + cost[i][k]);
  88. }
  89. fout << mn;
  90.  
  91. }
  92.  
  93. int main()
  94. {
  95. Read();
  96. Solve();
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement