题解/算法 {361. 观光奶牛}
@LINK: https://www.acwing.com/problem/content/description/363/
;
@DELI;
For the answer-loop, let its values on points be A 1 , A 2 , . . . , A n A1, A2, ..., An A1,A2,...,An, the weights on edges be E 1 , E 2 , . . . , E n E1, E2, ..., En E1,E2,...,En, then its worth is A 1 + . . . + A n E 1 + . . . + E n = k \frac{A1 +... +An}{E1 + ... +En} = k E1+...+EnA1+...+An=k
If we set a function C ( x ) C(x) C(x) denotes that whether there exists a loops such that its worth ≥ x \geq x ≥x, then we have C ( ≥ k ) = t r u e C( \geq k) = true C(≥k)=true, C ( < k ) = f a l s e C( < k) = false C(<k)=false (satisfying the Dichotomy-Method)
If
C
(
x
)
=
t
r
u
e
C(x) = true
C(x)=true, it shows that there is a loop
A
1
+
.
.
.
+
A
i
E
1
+
.
.
.
+
E
i
≥
k
\frac{A1 +... +Ai}{E1 + ... +Ei} \geq k
E1+...+EiA1+...+Ai≥k
A
1
+
.
.
.
+
A
i
≥
k
∗
E
1
+
.
.
.
+
k
∗
E
i
A1 + ... + Ai \geq k * E1 + ... + k * Ei
A1+...+Ai≥k∗E1+...+k∗Ei
(
A
1
−
k
∗
E
1
)
+
.
.
.
+
(
A
i
−
k
∗
E
i
)
≥
0
(A1 - k * E1) + ... + (Ai - k * Ei) \geq 0
(A1−k∗E1)+...+(Ai−k∗Ei)≥0
When we at the process
C
(
x
)
C(x)
C(x), we can construct a graph
G
G
G whose edge-weight of
a
→
b
a \to b
a→b equals
A
[
a
]
−
x
∗
E
[
a
→
b
]
A[ a] - x * E[ a \to b]
A[a]−x∗E[a→b]
If
C
(
x
)
=
t
r
u
e
C(x) = true
C(x)=true, then that is equivalent to
G
G
G exits a Positive-Loop;
This can be done by SPFA-Check-Loop for calculating the longest-path (not shortest), set all Distances be
0
0
0 initially.