-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1162.cpp
83 lines (66 loc) · 2.12 KB
/
1162.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
/**
source:https://acm.timus.ru/problem.aspx?num=1162
Пояснения к примененному алгоритму:
Идея задачи похожа на задачу 1450. Принципиальное отличие состоит в том, что при определении максимума требуется так же учитывать коммиссию за обмен.
Использовался алгоритм Беллмана-Форда.
*/
#include <iostream>
#include <vector>
#define BORDER 0.0001
using namespace std;
template<class S, class T>
class Graph
{
struct edge
{
S a, b;
T rate, commission;
};
vector<edge> edges;
vector<T> path;
S nof_nodes, nof_edges;
S start;
T sum;
public:
explicit Graph(S nof_nodes, S nof_edges, S currency, T sum): nof_nodes(nof_nodes), nof_edges(nof_edges), start(currency), sum(sum)
{
path = vector<T>(nof_nodes * 2);
path[currency] = sum;
}
void insert(S a, S b, T rate, T commission)
{
edges.push_back({a, b, rate, commission});
}
bool solve()
{
for (S i = 0; i < nof_nodes - 1; i++)
for (auto cur : edges)
if (path[cur.b] - (path[cur.a] - cur.commission) * cur.rate < BORDER)
path[cur.b] = (path[cur.a] - cur.commission) * cur.rate;
for (auto cur : edges)
if ((path[cur.a] - cur.commission) * cur.rate - path[cur.b] > BORDER)
return true;
return false;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
uint16_t nof_nodes, nof_edges, a, b, currency;
double rab, rba, cab, cba, sum;
cin >> nof_nodes >> nof_edges >> currency >> sum;
Graph<uint16_t, double> graph = Graph<uint16_t, double>(nof_nodes, nof_edges, currency, sum);
for (uint16_t i = 0; i < nof_edges; i++)
{
cin >> a >> b >> rab >> cab >> rba >> cba;
graph.insert(a, b, rab, cab);
graph.insert(b, a, rba, cba);
}
if ((graph.solve()))
cout << "YES";
else
cout << "NO";
return 0;
}