This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
columbia.cpp
75 lines (71 loc) · 1.57 KB
/
columbia.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
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int INF = 1e8;
int r, c;
vector<vector<int>> cost;
vector<vector<pair<int, int>>> adj;
const vector<pair<int, int>> neighbors = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
vector<int> d;
void solve()
{
d.assign(r * c, INF);
d[0] = 0;
set<pair<int, int>> s;
s.emplace(0, 0);
while (!s.empty())
{
auto u = s.begin()->second;
s.erase(s.begin());
for (auto [v, w] : adj[u])
{
// p(v, w)
if (d[u] + w < d[v])
{
s.erase({d[v], v});
d[v] = d[u] + w;
s.emplace(d[v], v);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> r >> c;
cost.assign(r, vector<int>(c));
adj.assign(r * c, vector<pair<int, int>>());
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cin >> cost[i][j];
}
}
cost[0][0] = 0;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
for (auto [dx, dy] : neighbors)
{
int ni = i + dy, nj = j + dx;
if (0 <= ni && ni < r && 0 <= nj && nj < c)
{
adj[ni * c + nj].emplace_back(i * c + j, cost[i][j]);
}
}
}
}
solve();
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cout << d[i * c + j] << ' ';
}
cout << '\n';
}
}