-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP423.cpp
78 lines (67 loc) · 1.74 KB
/
P423.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
typedef PI WeightAndVertex;
#define INF -1
/*
Dijkstra for shortest path.
N vertices
M edges
*/
int dijkstra(int N, std::vector<WeightAndVertex> *adjacencyLists, int source) {
bool *visited = new bool[N];
unsigned int *minPath = new unsigned int[N];
for(int i = 0; i < N; ++i) {
visited[i] = false;
minPath[i] = -1;
}
minPath[source] = 0;
int max = 0;
std::set<WeightAndVertex> Q; // To visit
Q.insert(WeightAndVertex(0, 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;
// perform relaxation:
visited[from] = true;
if(weight > max)
max = weight;
for(std::vector<WeightAndVertex>::const_iterator it = adjacencyLists[from].begin(); it != adjacencyLists[from].end(); ++it) {
int neighbour = it->second;
if(visited[neighbour])
continue;
int neighbourWeight = weight + it->first;
if(minPath[neighbour] <= neighbourWeight)
continue;
minPath[neighbour] = neighbourWeight;
Q.insert(WeightAndVertex(neighbourWeight, neighbour));
}
}
delete[] visited;
delete[] minPath;
return max;
}
/*
Widest path problem / the bottleneck shortest path problem
*/
int main() {
int N;
string cost;;
while(std::cin >> N) {
std::vector<WeightAndVertex> *adjacencyLists = new std::vector<WeightAndVertex>[N];
FORI(N) {
FORJ(i) {
cin >> cost;
if(cost[0] == 'x')
continue;
int c = atoi(cost.c_str());
adjacencyLists[i].push_back(WeightAndVertex(c, j));
adjacencyLists[j].push_back(WeightAndVertex(c, i));
}
}
cout << dijkstra(N, adjacencyLists, 0) << endl;
delete[] adjacencyLists;
}
return 0;
}