Advertisement
Arrias

Untitled

May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 6001;
  6.  
  7. typedef pair<int, pair<int, int>> super;
  8.  
  9. int n, k, d;
  10. vector <int> a;
  11.  
  12. struct node {
  13. node() : dp(INT_MAX), pre(-1) {}
  14. int dp;
  15. int pre;
  16. };
  17.  
  18. vector <vector<node>> DP;
  19.  
  20. void dijkstra() {
  21.  
  22. priority_queue < super, vector<super>, greater<super> > que;
  23.  
  24. for (int i = 1; i <= n; ++i) {
  25. DP[1][i].dp = a[i];
  26. DP[1][i].pre = -1;
  27. if (1 < k) {
  28. que.push({DP[1][i].dp,{1,i}});
  29. }
  30. }
  31.  
  32. while (!que.empty()) {
  33. pair<int, pair<int, int>> top = que.top();
  34. que.pop();
  35. int index = top.second.second;
  36. int cnt = k - top.second.first - 1;
  37. int CNT = top.second.first;
  38.  
  39. if (DP[CNT][index].dp != top.first) continue;
  40.  
  41. int lyl = n - cnt;
  42.  
  43. for (int i = index + 1; i <= lyl; ++i) {
  44. int cost = DP[CNT][index].dp + (i - index - 1)*d + a[i];
  45. if (DP[CNT + 1][i].dp > cost) {
  46. DP[CNT + 1][i].dp = cost;
  47. DP[CNT + 1][i].pre = index;
  48. if (CNT + 1 < k) {
  49. que.push({cost,{CNT + 1, i}});
  50. }
  51. }
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58. signed main() {
  59. scanf("%d %d", &n, &k);
  60. n += k;
  61. a.resize(n + 1);
  62. DP.resize(n + 1);
  63. for (int i = 1; i <= n; ++i) {
  64. DP[i].resize(n + 1);
  65. }
  66. for (int i = 1; i <= n; ++i) {
  67. scanf("%d", &a[i]);
  68. }
  69. scanf("%d", &d);
  70. dijkstra();
  71. int MIN = INT_MAX;
  72. int q;
  73.  
  74. for (int i = k; i <= n; ++i) {
  75. if (DP[k][i].dp < MIN) {
  76. MIN = DP[k][i].dp;
  77. q = i;
  78. }
  79. }
  80.  
  81. cout << MIN << endl;
  82.  
  83. set <int> ans;
  84.  
  85. while (k) {
  86. ans.insert(q);
  87. q = DP[k][q].pre;
  88. k--;
  89. }
  90.  
  91. for (auto i : ans) {
  92. printf("%d ", i);
  93. }
  94.  
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement