-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP929.cpp
104 lines (90 loc) · 2.57 KB
/
P929.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
typedef PI WeightAndVertex;
#define INF numeric_limits<int>::max()
/*
Dijkstra for shortest path.
N vertices
M edges
*/
int dijkstra(int N, int width, int height, int *M, int source, int destination) {
//cerr << "Dijkstra(|V|=" << N << ",source=" << source << ",destination=" << destination << ")" << endl;
if(source == destination) {
return M[source];
}
bool *visited = new bool[N];
int *minPath = new int[N];
for(int i = 0; i < N; ++i) {
visited[i] = false;
minPath[i] = INF;
}
minPath[source] = M[source];
set<WeightAndVertex> Q; // To visit
Q.insert(WeightAndVertex(M[source], source));
while(!Q.empty()) {
const WeightAndVertex p = *Q.begin();
Q.erase(Q.begin());
const int from = p.second;
if(visited[from])
continue;
const int weight = p.first;
if(from == destination)
break;
// perform relaxation:
//cerr << " " << from << " w=" << weight << " => relax from " << minPath[from] << " to " << weight << endl;
visited[from] = true;
int y = from / width;
int x = from % width;
for(int y2 = y-1; y2 <= y+1; ++y2) {
if(y2 < 0 || y2 >= height)
continue;
int x2 = x;
if(x2 < 0 || x2 >= width)
continue;
int neighbour = y2*width+x2;
if(visited[neighbour])
continue;
int neighbourWeight = weight + M[neighbour];
if(minPath[neighbour] <= neighbourWeight)
continue;
//cerr << " " << from << "-->" << neighbour << " w=" << M[neighbour] << " => from " << minPath[neighbour] << " to " << neighbourWeight << endl;
minPath[neighbour] = neighbourWeight;
Q.insert(WeightAndVertex(neighbourWeight, neighbour));
}
for(int x2 = x-1; x2 <= x+1; ++x2) {
if(x2 < 0 || x2 >= width)
continue;
int y2 = y;
int neighbour = y2*width+x2;
if(visited[neighbour])
continue;
int neighbourWeight = weight + M[neighbour];
if(minPath[neighbour] <= neighbourWeight)
continue;
//cerr << " " << from << "-->" << neighbour << " w=" << M[neighbour] << " => from " << minPath[neighbour] << " to " << neighbourWeight << endl;
minPath[neighbour] = neighbourWeight;
Q.insert(WeightAndVertex(neighbourWeight, neighbour));
}
}
int ret = minPath[destination];
delete[] visited;
delete[] minPath;
return ret;
}
/*
Widest path problem / the bottleneck shortest path problem
*/
int main() {
int N, w, h;
FORCAS {
cin >> h >> w;
N = h*w;
int *M = new int[w*h];
FORY(h) {
FORX(w) {
cin >> M[y*w+x];
}
}
cout << dijkstra(N, w, h, M, 0, N-1) << endl;
delete[] M;
}
return 0;
}