Poj 1860 (Bellman_Ford judgment Center)

Source: Internet
Author: User

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 ;}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.