This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array
const int inf = 1e18;
void solve() {
int h, w; cin >> h >> w;
vector<int> ch(h), cv(w);
for (int i = 0; i < h; i++) cin >> ch[i];
for (int i = 0; i < w; i++) cin >> cv[i];
using node = pair<int, pair<int, int>>;
vector<vector<int>> dis(h, vector<int>(w,inf));
priority_queue<node, vector<node>, greater<node>> pq;
pq.push(mp(0, mp(0, 0)));
while (pq.size()) {
node p = pq.top(); pq.pop();
int x = p.second.first, y = p.second.second, pp = p.first;
if (dis[x][y] <= pp) continue;
dis[x][y] = pp;
if (x + 1 < h) pq.push(mp(pp + cv[y], mp(x + 1, y)));
if (y + 1 < w) pq.push(mp(pp + ch[x], mp(x, y + 1)));
}
cout << dis[h - 1][w - 1] << '\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |