Question: give n currencies, exchange relationships in m, give two currency exchange rates and service fees, and find out whether the exchange between currencies can increase wealth. Use Bellman_Ford to determine whether there is a positive ring. If there is a positive ring, the water can be relaxed infinitely, and the wealth can be increased infinitely.
# Include <string. h> # include <stdio. h> const int N = 110; const int inf = 0x3fffffff; int start, num, n; double dist [N], wf; struct edge {int st, ed; double cost, w;} e [220]; void addedge (int x, int y, double w, double c) {e [num]. st = x; e [num]. ed = y; e [num]. cost = c; e [num ++]. w = w;} int Bellman_Ford () {int flag = 0, I, u, v, j; for (I = 1; I <= n; I ++) dist [I] = 0; dist [start] = wf; for (I = 1; I <n; I ++) // n-1 relaxation {for (j = 0; j <num; j ++) {u = E [j]. st; v = e [j]. ed; if (dist [v] <(dist [u]-e [j]. cost) * e [j]. w) {dist [v] = (dist [u]-e [j]. cost) * e [j]. w; flag = 1 ;}}if (flag = 0) break;} for (I = 0; I <num; I ++) if (dist [e [I]. ed] <(dist [e [I]. st]-e [I]. cost) * e [I]. w) // return 1; return 0;} int main () {int m, I, x, y; double a, B, c, d; while (scanf ("% d % lf", & n, & m, & start, & wf )! =-1) {num = 0; for (I = 1; I <= m; I ++) {scanf ("% d % lf", & x, & y, & a, & B, & c, & d ); addedge (x, y, a, B); addedge (y, x, c, d);} if (Bellman_Ford () printf ("YES \ n "); else printf ("NO \ n");} return 0 ;}