Advertisement
nicuvlad76

Untitled

Feb 16th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin ("expeditie.in");
  6. ofstream fout ("expeditie.out");
  7.  
  8.  
  9. const short Nmax = 105;
  10. const short Kmax = 1005;
  11.  
  12. int dp[Nmax][Nmax][Kmax], n, m, a[Nmax][Nmax], t[Nmax][Nmax], k;
  13.  
  14. bool viz[Nmax][Nmax][Kmax];
  15.  
  16.  
  17. struct G
  18. {
  19.     int i, j, energie;
  20. };
  21.  
  22. queue < G > Q;
  23.  
  24.  
  25. int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  26. int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
  27.  
  28. inline int Bellman_Ford(int E)
  29. {
  30.     G w;
  31.     int c, x, y;
  32.     for(int i = 1 ; i <= n ; i++)
  33.         for(int j = 1 ; j <= m ; j++)
  34.             for(int pas = 0 ; pas <= E ; pas++)
  35.                 dp[i][j][pas] = (1 << 30), viz[i][j][pas] = false;
  36.     w = {1, 1, E};
  37.     Q.push(w);
  38.     viz[1][1][E] = true;
  39.     dp[1][1][E] = 0;
  40.     while(!Q . empty())
  41.     {
  42.         w = Q.front();
  43.         if(a[w.i][w.j] < 0)
  44.         {
  45.             dp[w.i][w.j][E] = dp[w.i][w.j][w.energie];
  46.             c = E;
  47.         }
  48.         else c = w.energie;
  49.         Q.pop();
  50.         viz[w.i][w.j][c] = false;
  51.         for(int f = 0 ; f < 8 ; f++)
  52.         {
  53.             x = w.i + dx[f];
  54.             y = w.j + dy[f];
  55.             if(!(1 <= x && x <= n && 1 <= y && y <= m))
  56.                 continue;
  57.             if(c >= abs(a[x][y]) && dp[x][y][c - abs(a[x][y])] > dp[w.i][w.j][c] + t[x][y])
  58.             {
  59.  
  60.                 dp[x][y][c - abs(a[x][y])] = dp[w.i][w.j][c] + t[x][y];
  61.                 if(!viz[x][y][c - abs(a[x][y])])
  62.                 {
  63.                     viz[x][y][c - abs(a[x][y])] = true;
  64.                     Q.push({x, y, c - abs(a[x][y])});
  65.                 }
  66.             }
  67.         }
  68.  
  69.     }
  70.     int minim = (1 << 30);
  71.     for(int i = 0 ; i <= E ; i++)
  72.         minim = min(minim, dp[n][m][i]);
  73.     return minim;
  74. }
  75.  
  76. int main()
  77. {
  78.     int st, dr, mij, poz = 0, x, c;
  79.     fin >> n >> m >> k;
  80.     for(int i = 1 ; i <= n ; i++)
  81.         for(int j = 1 ; j <= m ; j++)
  82.             fin >> a[i][j];
  83.     for(int i = 1 ; i <= n ; i++)
  84.         for(int j = 1 ; j <= m ; j++)
  85.             fin >> t[i][j];
  86.     x = Bellman_Ford(k);
  87.     if(x == (1 << 30))
  88.     {
  89.         fout << "Nu exista drum\n";
  90.         return 0;
  91.     }
  92.     fout << x << "\n";
  93.     st = 1, dr = k;
  94.     while(st <= dr)
  95.     {
  96.         mij = (st + dr) / 2;
  97.         c = Bellman_Ford(mij);
  98.         if(c == x)
  99.             poz = mij, dr = mij - 1;
  100.         else st = mij + 1;
  101.     }
  102.     fout << poz << "\n";
  103.     fin.close();
  104.     fout.close();
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement