POJ 1860 Currency Exchange

Source: Internet
Author: User

Test instructions: There are n kinds of currency, can be exchanged with each other, there are M redemption rules, exchange rules give the exchange rate R and handling fee C, the formula is B = (a-c) * R, from a currency to B currency, ask whether you can make money through constant conversion, the amount in the hands of the exchange cannot be negative.

Solution: Bellman-ford. Map: The currency as a point, each redemption rule for the edge, two points of the path length of the money after the exchange. After the map can be seen test instructions for the existence of positive ring in the figure, with Bellman-ford to find the longest path, if there is positive ring output Yes, there is no output.

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #include <iomanip> #define LL Long Long#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 |    1using namespace Std;struct node{int u, v;    Double R, C; node (int u, int v, double R, double C): U (U), V (v), R (R), C (c) {} node () {}}edge[205];int n, M, St, cnt;double money;d        Ouble Dis[105];bool Bellmanford () {for (int i = 1; I <= n; i++) {if (i = = st) Dis[i] = money; else Dis[i] = 0;//is initialized to 0 because there can be no negative amount in the process} for (int i = 1; i < n; i++)//n-1 times relaxation for (int j = 0; J < CNT;    ) DIS[EDGE[J].V] = max ((dis[edge[j].u]-edge[j].c) * EDGE[J].R, DIS[EDGE[J].V]); for (int i = 0; i < cnt; i++)//If you can relax after n-1, sayThe existence of positive ring if (DIS[EDGE[I].V] < (DIS[EDGE[I].U]-edge[i].c) * EDGE[I].R) return 1; return 0;}        int main () {while (~scanf ("%d%d%d%lf", &n, &m, &st, &money)) {cnt = 0;            for (int i = 0; i < m; i++) {int u, v;            Double R1, C1, R2, C2;            scanf ("%d%d%lf%lf%lf%lf", &u, &v, &r1, &c1, &AMP;R2, &AMP;C2);            edge[cnt++] = node (u, V, R1, C1);        edge[cnt++] = node (V, u, R2, C2);        } if (Bellmanford ()) puts ("YES");    Else puts ("NO"); } return 0;}

  

POJ 1860 Currency Exchange

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.