forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path82.cpp
More file actions
81 lines (72 loc) · 1.98 KB
/
82.cpp
File metadata and controls
81 lines (72 loc) · 1.98 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
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define MAXN 80
vector<string> split( const string& s, const string& delim =" " ) {
vector<string> res;
string t;
for ( int i = 0 ; i != s.size() ; i++ ) {
if ( delim.find( s[i] ) != string::npos ) {
if ( !t.empty() ) {
res.push_back( t );
t = "";
}
} else {
t += s[i];
}
}
if ( !t.empty() ) {
res.push_back(t);
}
return res;
}
vector<int> splitInt( const string& s, const string& delim =" " ) {
vector<string> tok = split( s, delim );
vector<int> res;
for ( int i = 0 ; i != tok.size(); i++ )
res.push_back( atoi( tok[i].c_str() ) );
return res;
}
vector<vector<int> > num;
int mm[MAXN + 5][MAXN + 5];
class state {
public:
int row, col, dist;
state(int r, int c, int d) : row(r), col(c), dist(d) {};
bool operator<(const state &rhs) const {
return dist > rhs.dist;
}
};
void run() {
REP(i,MAXN) {
string str;
cin >> str;
num.push_back(splitInt(str, ","));
}
memset(mm, 127, sizeof(mm));
priority_queue<state> pq;
REP(i, MAXN) {
mm[i][MAXN - 1] = num[i][MAXN - 1];
pq.push(state(i, MAXN - 1, num[i][MAXN - 1]));
}
while (!pq.empty()) {
state cur = pq.top(); pq.pop();
int dist = cur.dist, row = cur.row, col = cur.col;
if (dist > mm[row][col]) continue;
else mm[row][col] = dist;
if (col > 0) pq.push(state(row, col - 1, num[row][col - 1] + dist));
if (row > 0) pq.push(state(row - 1, col, num[row - 1][col] + dist));
if (row < MAXN - 1) pq.push(state(row + 1, col, num[row + 1][col] + dist));
}
int ret = mm[0][0];
FOR(i,1,MAXN-1) ret = min(ret, mm[i][0]);
cout << ret << endl;
}
int main() {
run();
return 0;
}