-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path83.cpp
86 lines (74 loc) · 1.92 KB
/
83.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <tuple>
#include "81_82_83.h"
const std::vector<std::pair<int, int>> D = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void dfs() {
Matrix m = read_matrix();
int s = m.size();
Matrix dp(s, std::vector<int>(s, INT_MAX));
dp[0][0] = m[0][0];
std::vector<std::tuple<int, int, int>> stack;
stack.push_back({dp[0][0], 0, 0});
while (!stack.empty()) {
auto [sum, i, j] = stack.back();
stack.pop_back();
for (auto &[di, dj] : D) {
int ii = i + di, jj = j + dj;
if (ii < 0 || ii >= s || jj < 0 || jj >= s) continue;
int new_sum = sum + m[ii][jj];
if (new_sum >= dp[ii][jj]) continue;
dp[ii][jj] = new_sum;
stack.push_back({new_sum, ii, jj});
}
}
std::cout << dp[s - 1][s - 1] << std::endl;
}
std::pair<int, int> get_min_dist(Matrix &dist, std::vector<std::pair<int, int>> &Q) {
int min_idx = 0;
int min_dist = INT_MAX;
for (int k = 0; k < Q.size(); ++k) {
auto &[i, j] = Q[k];
if (dist[i][j] < min_dist) {
min_dist = dist[i][j];
min_idx = k;
}
}
std::swap(Q[min_idx], Q.back());
auto ans = Q.back();
Q.pop_back();
return ans;
}
bool in_Q(std::pair<int, int> lhs, std::vector<std::pair<int, int>> &Q) {
for (auto &rhs : Q) {
if (lhs == rhs) return true;
}
return false;
}
void dijkstra() {
Matrix m = read_matrix();
int s = m.size();
Matrix dist(s, std::vector<int>(s, INT_MAX));
dist[0][0] = m[0][0];
std::vector<std::pair<int, int>> Q;
for (int i = 0; i < s; ++i) {
for (int j = 0; j < s; ++j) Q.push_back({i, j});
}
while (!Q.empty()) {
auto [i, j] = get_min_dist(dist, Q);
for (auto &[di, dj] : D) {
int ii = i + di, jj = j + dj;
if (ii < 0 || ii >= s || jj < 0 || jj >= s) continue;
if (!in_Q({ii, jj}, Q)) continue;
int alt = dist[i][j] + m[ii][jj];
if (alt >= dist[ii][jj]) continue;
dist[ii][jj] = alt;
}
}
std::cout << dist[s - 1][s - 1] << std::endl;
}
int main() {
dijkstra();
}