POJ-1860 currency exchange Bellman

Source: Internet
Author: User

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

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.