Skip to content

Commit 59ae590

Browse files
committed
Today ps
1 parent 0ff2527 commit 59ae590

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Baekjoon/Cpp/4485.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
#define INF 2147483647
3+
4+
using namespace std;
5+
using pii = pair<int, int>;
6+
using pipii = pair<int, pii>;
7+
8+
struct compare
9+
{
10+
bool operator()(pipii &a, pipii &b)
11+
{
12+
return a.first > b.first;
13+
}
14+
};
15+
16+
int main()
17+
{
18+
ios::sync_with_stdio(0);
19+
cin.tie(0);
20+
int n, i, j, d, gph[126][126], dis[126][126], dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
21+
priority_queue<pipii, vector<pipii>, compare> pq;
22+
cin >> n;
23+
d = 0;
24+
while (n != 0)
25+
{
26+
d += 1;
27+
for (i = 0; i < n; i++)
28+
{
29+
for (j = 0; j < n; j++)
30+
{
31+
cin >> gph[i][j];
32+
dis[i][j] = INF;
33+
}
34+
}
35+
dis[0][0] = 0;
36+
pq.push(make_pair(gph[0][0], make_pair(0, 0)));
37+
while (!pq.empty())
38+
{
39+
pipii c = pq.top();
40+
pii cv = c.second;
41+
pq.pop();
42+
if (cv.first == n - 1 && cv.second == n - 1)
43+
{
44+
continue;
45+
}
46+
for (j = 0; j < 4; j++)
47+
{
48+
pii nv = make_pair(cv.first + dx[j], cv.second + dy[j]);
49+
if (nv.first >= 0 && nv.first < n && nv.second >= 0 && nv.second < n && c.first + gph[nv.first][nv.second] < dis[nv.first][nv.second])
50+
{
51+
dis[nv.first][nv.second] = c.first + gph[nv.first][nv.second];
52+
pq.push(make_pair(dis[nv.first][nv.second], nv));
53+
}
54+
}
55+
}
56+
cout << "Problem " << d << ": " << dis[n - 1][n - 1] << "\n";
57+
cin >> n;
58+
}
59+
return 0;
60+
}

0 commit comments

Comments
 (0)