/
aoj-0304.test.cpp
68 lines (58 loc) · 1.5 KB
/
aoj-0304.test.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
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0304"
//
#include "../../template/template.hpp"
//
#include "../../shortest-path/dual-of-shortest-path.hpp"
using namespace Nyaan;
void Nyaan::solve() {
ini(n, c);
using T = tuple<ll, string, ll, string, ll>;
vector<T> cons;
vector<int> undef;
rep(_, c) {
ins(s);
ll a = 0, b = 0, d = 0;
string o, e;
int i = 0;
while (isdigit(s[i])) a = a * 10 + s[i++] - '0';
while (!isdigit(s[i])) o.push_back(s[i++]);
while (isdigit(s[i])) b = b * 10 + s[i++] - '0';
while (!isdigit(s[i])) e.push_back(s[i++]);
while (isdigit(s[i])) d = d * 10 + s[i++] - '0';
--a, --b;
cons.emplace_back(a, o, b, e, d);
if (o == "*") undef.push_back(_);
}
ll ans = -1;
for (int bt = 0; bt < (1 << undef.size()); bt++) {
rep(i, sz(undef)) get<1>(cons[undef[i]]) = gbit(bt, i) ? ">=" : "<=";
Dual_of_Shortest_Path<ll> g(n);
for (auto&& [a, o, b, e, d] : cons) {
if (o == "<=") {
g.add_edge(b, a, 0);
if (e == "+") {
g.add_edge(b, a, -d);
} else {
g.add_edge(a, b, d);
}
}
if (o == ">=") {
g.add_edge(a, b, 0);
if (e == "+") {
g.add_edge(a, b, -d);
} else {
g.add_edge(b, a, d);
}
}
}
auto d = g.solve();
if (sz(d)) {
if (*min_element(all(d)) < 0) continue;
amax(ans, *max_element(all(d)));
}
}
if (ans > 1e18)
out("inf");
else
out(ans);
}