-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObstacleCourse318.cpp
More file actions
71 lines (70 loc) · 1.45 KB
/
ObstacleCourse318.cpp
File metadata and controls
71 lines (70 loc) · 1.45 KB
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
#include <iostream>
#include <queue>
#define INF 0x5FFFFFFF;
using namespace std;
class node {
public:
int row;
int column;
int distance;
node(int, int, int);
bool operator<(const node& a)const {//min priority queue
return distance > a.distance;
}
};
node::node(int a, int b, int d) {
row = a;
column = b;
distance = d;
}
int arr[125][125];
int dist[125][125];
priority_queue <node> pq;
int n = 0;
void relax(int r1, int c1, int r2, int c2) {// relax from r1,c1 to r2,c2
if (dist[r2][c2] > dist[r1][c1] + arr[r2][c2]) {
dist[r2][c2] = dist[r1][c1] + arr[r2][c2];
node newNode(r2, c2, dist[r2][c2]);
pq.push(newNode);
}
}
void dijkstras() {
while (!pq.empty()) {
node u = pq.top();
if (u.row == u.column && u.row == n - 1)
return;
pq.pop();
if (u.row != 0) {
relax(u.row, u.column, (u.row - 1), u.column);
}
if (u.column != 0) {
relax(u.row, u.column, u.row , (u.column - 1));
}
if (u.row != n - 1) {
relax(u.row, u.column, (u.row + 1), u.column);
}
if (u.column != n-1) {
relax(u.row, u.column, u.row, (u.column + 1));
}
}
}
int main() {
int ctr = 1;
while (cin >> n) {
if (n == 0)
return 0;
pq = priority_queue<node>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
dist[i][j] = INF;
}
}
dist[0][0] = arr[0][0];
node start(0, 0, dist[0][0]);
pq.push(start);
dijkstras();
cout << "Problem " << ctr++ << ": " << dist[n - 1][n - 1] << endl;
}
return 0;
}