Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin ("expeditie.in");
- ofstream fout ("expeditie.out");
- const short Nmax = 105;
- const short Kmax = 1005;
- int dp[Nmax][Nmax][Kmax], n, m, a[Nmax][Nmax], t[Nmax][Nmax], k;
- bool viz[Nmax][Nmax][Kmax];
- struct G
- {
- int i, j, energie;
- };
- queue < G > Q;
- int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
- int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
- inline int Bellman_Ford(int E)
- {
- G w;
- int c, x, y;
- for(int i = 1 ; i <= n ; i++)
- for(int j = 1 ; j <= m ; j++)
- for(int pas = 0 ; pas <= E ; pas++)
- dp[i][j][pas] = (1 << 30), viz[i][j][pas] = false;
- w = {1, 1, E};
- Q.push(w);
- viz[1][1][E] = true;
- dp[1][1][E] = 0;
- while(!Q . empty())
- {
- w = Q.front();
- if(a[w.i][w.j] < 0)
- {
- dp[w.i][w.j][E] = dp[w.i][w.j][w.energie];
- c = E;
- }
- else c = w.energie;
- Q.pop();
- viz[w.i][w.j][c] = false;
- for(int f = 0 ; f < 8 ; f++)
- {
- x = w.i + dx[f];
- y = w.j + dy[f];
- if(!(1 <= x && x <= n && 1 <= y && y <= m))
- continue;
- if(c >= abs(a[x][y]) && dp[x][y][c - abs(a[x][y])] > dp[w.i][w.j][c] + t[x][y])
- {
- dp[x][y][c - abs(a[x][y])] = dp[w.i][w.j][c] + t[x][y];
- if(!viz[x][y][c - abs(a[x][y])])
- {
- viz[x][y][c - abs(a[x][y])] = true;
- Q.push({x, y, c - abs(a[x][y])});
- }
- }
- }
- }
- int minim = (1 << 30);
- for(int i = 0 ; i <= E ; i++)
- minim = min(minim, dp[n][m][i]);
- return minim;
- }
- int main()
- {
- int st, dr, mij, poz = 0, x, c;
- fin >> n >> m >> k;
- for(int i = 1 ; i <= n ; i++)
- for(int j = 1 ; j <= m ; j++)
- fin >> a[i][j];
- for(int i = 1 ; i <= n ; i++)
- for(int j = 1 ; j <= m ; j++)
- fin >> t[i][j];
- x = Bellman_Ford(k);
- if(x == (1 << 30))
- {
- fout << "Nu exista drum\n";
- return 0;
- }
- fout << x << "\n";
- st = 1, dr = k;
- while(st <= dr)
- {
- mij = (st + dr) / 2;
- c = Bellman_Ford(mij);
- if(c == x)
- poz = mij, dr = mij - 1;
- else st = mij + 1;
- }
- fout << poz << "\n";
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement