-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathford_bellman.cpp
101 lines (77 loc) · 2.39 KB
/
ford_bellman.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
#include <iostream>
#include <vector>
using namespace std;
struct Edge{
int src, dest;
double weight_s_d, weight_d_s;
Edge(int src, int dest, double weight1, double weight2){
this->src = src;
this->dest = dest;
this->weight_s_d = weight1;
this->weight_d_s = weight2;
}
};
class Graph{
public:
vector <Edge> edges;
double * dist;
int v;
Graph(int vertex_num){
dist = new double[vertex_num];
v = vertex_num;
}
void ford_bellman(){
for(int j = 1; j < v - 1; j++){
bool test = false;
for(int i = 0; i < edges.size(); i++){
if(dist[edges[i].dest] < dist[edges[i].src] - dist[edges[i].src] * edges[i].weight_s_d){
test = true;
dist[edges[i].dest] = dist[edges[i].src] - dist[edges[i].src] * edges[i].weight_s_d;
}
if(dist[edges[i].src] < dist[edges[i].dest] - dist[edges[i].dest] * edges[i].weight_d_s){
test = true;
dist[edges[i].src] = dist[edges[i].dest] - dist[edges[i].dest] * edges[i].weight_d_s;
}
}
if(!test)
break;
}
}
};
int main(){
std::ios_base::sync_with_stdio(false);
int exchanges_num, has_bc, edges_num, v1, v2, start;
double weight1, weight2, max;
bool negative_cycle = false;
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);
g.dist[i] = 0.0;
}
cin >> edges_num;
for(int i = 0; i < edges_num; i++){
cin >> v1 >> v2 >> weight1 >> weight2;
Edge e(v1, v2, weight1, weight2);
g.edges.push_back(e);
if(weight1 + weight2 < 0)
negative_cycle = true;
}
cin >> start;
if(negative_cycle){
cout << "MATEUSZU NIE KOMBINUJ!" << endl;
return 0;
}
g.dist[start] = 100000.0;
g.ford_bellman();
for(int i = 0; i < bajtcoin_list.size(); i++){
g.dist[ bajtcoin_list[i] ] = 1.2 * g.dist[ bajtcoin_list[i] ];
}
g.ford_bellman();
max = g.dist[start];
cout << static_cast<int>(max) << endl;
return 0;
}