This question is to give a currency and then give some exchange conditions. Can it make the total number of coins increase? Is it very attractive?
One transformation to this question is that if a ring occurs during the conversion process, we can continuously convert the ring, resulting in an infinite number of coins, in turn, you can get the original currency, and it will definitely increase.
The bellman algorithm can be used to determine whether a ring exists. Because the path length of the longest ring is N-1 (N nodes), we only need to perform N-1 relaxation on all edges, then, let's see if we can continue to relax to determine whether a ring is formed.
The Code is as follows:
# Include <cstdlib> # include <cstring> # include <cstdio> # define maxn 205 using namespace STD; int n, m, S, CNT; Double V, DIS [maxn]; struct edge {int A, B; Double R, C;} e [maxn]; bool Bellman () {memset (DIS, 0, sizeof (DIS); memset (hash, 0, sizeof (hash); DIS [s] = V; hash [s] = 1; for (Int J = 1; j <= N-1; ++ J) {for (INT I = 1; I <= CNT; ++ I) {// traverse all edges if (DIS [E [I]. a]-E [I]. c) * E [I]. r-Dis [E [I]. b]> 1e-6) {dis [E [I]. b] = (DIS [E [I]. a]-E [I]. c) * E [I]. r;
// Visit cannot be used here to determine whether a ring is formed, because it may be updated twice in an update }}for (INT I = 1; I <= CNT; ++ I) {If (DIS [E [I]. a]-E [I]. c) * E [I]. r-Dis [E [I]. b]> 1e-6) {return 1 ;}} return 0 ;}int main () {int A, B; double Rab, cab, RBA, CBA; while (scanf ("% d % lf", & N, & M, & S, & V) = 4) {CNT = 0; for (INT I = 0; I <m; ++ I) {scanf ("% d % lf", & A, & B, & Rab, & cab, & RBA, & CBA); ++ CNT; E [CNT]. A = A, E [CNT]. B = B, E [CNT]. R = Rab, E [CNT]. C = cab; ++ CNT; E [CNT]. A = B, E [CNT]. B = A, E [CNT]. R = RBA, E [CNT]. C = CBA;} printf (bellman ()? "Yes \ n": "No \ n ");}}