-
Notifications
You must be signed in to change notification settings - Fork 1
/
dijkstra_shortest_path .cpp
127 lines (97 loc) · 3.13 KB
/
dijkstra_shortest_path .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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct Edge{
int v;
double weight1, weight2;
Edge(int v1, double w1, double w2){
v = v1;
weight1 = w1;
weight2 = w2;
}
};
struct Vertex{
double dist1, dist2;
vector<Edge> edges;
};
class Graph{
public:
vector<Vertex> adj_list;
Graph(int size){
for(int i = 0; i < size; i++)
adj_list.push_back(Vertex());
}
void dijkstraFirst(int start){
set <int> vertex_set;
for(int i = 0; i < adj_list.size(); i++){
vertex_set.insert(i);
adj_list[i].dist1 = 0;
}
adj_list[start].dist1 = 1;
while(!vertex_set.empty()){
int min_dist = -1;
for (int i : vertex_set)
if (min_dist == -1 || adj_list[min_dist].dist1 < adj_list[i].dist1)
min_dist = i;
vertex_set.erase(min_dist);
for(Edge q : adj_list[min_dist].edges){
double dist = q.weight1 * adj_list[min_dist].dist1;
if (dist > adj_list[q.v].dist1)
adj_list[q.v].dist1 = dist;
}
}
}
void dijkstraSecond(int start){
set <int> vertex_set;
for(int i = 0; i < adj_list.size(); i++){
vertex_set.insert(i);
adj_list[i].dist2 = 0;
}
adj_list[start].dist2 = 1;
while(!vertex_set.empty()){
int min_dist = -1;
for (int i : vertex_set)
if (min_dist == -1 || adj_list[min_dist].dist2 < adj_list[i].dist2)
min_dist = i;
vertex_set.erase(min_dist);
for(Edge q : adj_list[min_dist].edges){
double dist = q.weight2 * adj_list[min_dist].dist2;
if (dist > adj_list[q.v].dist2)
adj_list[q.v].dist2 = dist;
}
}
}
};
int main(){
std::ios_base::sync_with_stdio(false);
int exchanges_num, has_bc, edges_num, v1, v2, start;
double weight1, weight2, max;
vector <int> bajtcoin_list;
cin >> exchanges_num;
Graph g(exchanges_num);
for(int i = 0; i < exchanges_num; i++){
cin >> has_bc;
if(has_bc == 1)
bajtcoin_list.push_back(i);
}
cin >> edges_num;
for(int i = 0; i < edges_num; i++){
cin >> v1 >> v2 >> weight1 >> weight2;
Edge e1(v1, 1 - weight1, 1 - weight2);
g.adj_list[v2].edges.push_back(e1);
Edge e2(v2, 1 - weight1, 1 - weight2);
g.adj_list[v1].edges.push_back(e2);
}
cin >> start;
g.dijkstraFirst(start);
g.dijkstraSecond(start);
max = 1.2 * (g.adj_list[bajtcoin_list[0]].dist1 * g.adj_list[bajtcoin_list[0]].dist2);
for(int i = 1; i < bajtcoin_list.size(); i++){
int j = bajtcoin_list[i];
if (1.2 * (g.adj_list[j].dist1 * g.adj_list[j].dist2) > max)
max = 1.2 * (g.adj_list[j].dist1 * g.adj_list[j].dist2);
}
cout << 100000 * max << endl;
return 0;
}