Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define taskname "TEST"
- typedef long long ll;
- typedef long double ld;
- typedef pair <ll, int> lli;
- #define fi first
- #define se second
- #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
- const int N = 3e4 + 5;
- const ll INF = 1e18;
- ll d[N];
- int n, m;
- vector <lli> adj[N];
- void inp() {
- cin >> m >> n;
- for (int i = 1; i <= m; i++) {
- int w;
- for (int j = 1; j <= n; j++) {
- cin >> w;
- adj[i*n + j].push_back(lli(w, (i - 1)*n + j));
- }
- for (int j = 1; j < n; j++) {
- cin >> w;
- adj[i*n + j].push_back(lli(w, i*n + j + 1));
- adj[i*n + j + 1].push_back(lli(w, i*n + j));
- }
- }
- }
- void dijktra() {
- priority_queue <lli, vector <lli>, greater <lli> > pq;
- for (int i = 1; i <= n*m + n; i++) d[i] = INF;
- d[m*n + n] = 0;
- pq.push(lli(0, m*n + n));
- while (pq.size()) {
- lli t = pq.top();
- pq.pop();
- int u = t.se;
- if (d[u] < t.fi) continue;
- for (lli t: adj[u])
- if (d[t.se] > d[u] + t.fi) {
- d[t.se] = d[u] + t.fi;
- pq.push(lli(d[t.se], t.se));
- }
- }
- }
- void out() {
- cout << *min_element(d + 1, d + n + 1);
- }
- int main() {
- fast_io;
- // freopen(taskname".INP", "r", stdin);
- // freopen(taskname".OUT", "w", stdout);
- inp();
- dijktra();
- out();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement