a53

expeditie

a53
Oct 31st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <queue>
  4. #include <cmath>
  5. #include <limits>
  6.  
  7. #define pb push_back
  8. #define mp make_pair
  9. #define NMAX 100
  10. #define KMAX 1000
  11.  
  12. using namespace std;
  13.  
  14. ifstream fin("expeditie.in");
  15. ofstream fout("expeditie.out");
  16.  
  17. int dp[NMAX][NMAX][KMAX];
  18. bool inQueue[NMAX][NMAX][KMAX];
  19.  
  20. int n, m, k;
  21. int a[NMAX][NMAX];
  22. int T[NMAX][NMAX];
  23.  
  24. queue<pair<pair<int, int>, int> > q;
  25. pair<pair<int, int>, int> curr;
  26.  
  27. int INF = numeric_limits<int>::max();
  28.  
  29. int x[] = {-1, -1, 0, 1, 1, 1, 0, -1}, y[] = {0, 1, 1, 1, 0, -1, -1, -1};
  30.  
  31. void Read ()
  32. {
  33. fin >> n >> m >> k;
  34.  
  35. int i, j;
  36. for (i = 1; i <= n; ++i)
  37. for (j = 1; j <= m; ++j)
  38. fin >> a[i][j];
  39.  
  40. for (i = 1; i <= n; ++i)
  41. for (j = 1; j <= m; ++j)
  42. fin >> T[i][j];
  43. }
  44.  
  45. bool Verif (int i, int j)
  46. {
  47. if (i >= 1 && i <= n && j >= 1 && j <= m) return true;
  48. return false;
  49. }
  50.  
  51. int BF(int cap)
  52. {
  53. int i, j, s;
  54. int time, capacity;
  55. pair<int,int> vt;
  56.  
  57. for(i = 1; i <= n; ++i)
  58. for (j = 1; j <= m; ++j)
  59. for(s = 0; s <= cap; ++s)
  60. {
  61. dp[i][j][s] = INF;
  62. inQueue[i][j][s] = false;
  63. }
  64. q.push({{1,1},cap});
  65. inQueue[1][1][cap] = true;
  66. dp[1][1][cap] = 0;
  67.  
  68. while(!q.empty())
  69. {
  70. curr = q.front();
  71. if (a[curr.first.first][curr.first.second] < 0)
  72. {
  73. dp[curr.first.first][curr.first.second][cap] = dp[curr.first.first][curr.first.second][curr.second];
  74. curr.second = cap;
  75. }
  76. for (i = 0; i < 8; ++i)
  77. {
  78. if (Verif(curr.first.first + x[i], curr.first.second + y[i]))
  79. {
  80. vt.first = curr.first.first + x[i];
  81. vt.second = curr.first.second + y[i];
  82. time = T[vt.first][vt.second];
  83. capacity = abs(a[vt.first][vt.second]);
  84.  
  85. if (capacity <= curr.second && dp[curr.first.first][curr.first.second][curr.second] + time < dp[vt.first][vt.second][curr.second - capacity])
  86. {
  87. dp[vt.first][vt.second][curr.second - capacity] = dp[curr.first.first][curr.first.second][curr.second] + time;
  88. if (inQueue[vt.first][vt.second][curr.second - capacity] == false)
  89. {
  90. inQueue[vt.first][vt.second][curr.second - capacity] = true;
  91. q.push({{vt.first, vt.second}, curr.second - capacity});
  92. }
  93. }
  94. }
  95. }
  96. q.pop();
  97. inQueue[curr.first.first][curr.first.second][curr.second] = false;
  98. }
  99.  
  100. int minim = INF;
  101. for(i = 0; i <= cap; ++i)
  102. if(dp[n][m][i] < minim) minim = dp[n][m][i];
  103. return minim;
  104. }
  105.  
  106. void Solve ()
  107. {
  108. int left, right, mid;
  109. int distMin = BF(k);
  110.  
  111. if (distMin == INF) fout << "Nu exista drum\n";
  112. else
  113. {
  114. left = 1;
  115. right = k;
  116. while(left <= right)
  117. {
  118. mid = (left + right) >> 1;
  119. if (BF(mid) == distMin) right = mid - 1;
  120. else left = mid + 1;
  121. }
  122.  
  123. fout << distMin << "\n" << left;
  124. }
  125. }
  126. int main ()
  127. {
  128. Read();
  129. Solve();
  130.  
  131. return 0;
  132. }
Add Comment
Please, Sign In to add comment