zoukankan      html  css  js  c++  java
  • POJ1860——Currency Exchange(BellmanFord算法求最短路)

    Currency Exchange

    Description
    Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
    For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
    You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively.
    Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.
    Input
    The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
    For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
    Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.
    Output
    If Nick can increase his wealth, output YES, in other case output NO to the output file.
    Sample Input
    3 2 1 20.0
    1 2 1.00 1.00 1.00 1.00
    2 3 1.10 1.00 1.10 1.00
    Sample Output
    YES

    题目大意:

        给定一种货币的本金。再给定一些货币之间转换的汇率和手续费。判断是否可以通过货币之间的转换来使本金变多。

    解题思路:

        根据给定的货币转换建立有向图。

        使用Bellman-Ford算法对有向图进行N-1次松弛。 (N为顶点个数)

        若一个图不存在正权回路,则最多进行N-1次松弛,若存在正权回路,则可以再进行松弛。    

    Code:

     1 /*************************************************************************
     2     > File Name: poj1860.cpp
     3     > Author: Enumz
     4     > Mail: 369372123@qq.com 
     5     > Created Time: 2014年10月17日 星期五 17时08分07秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<cstdio>
    10 #include<cstdlib>
    11 #include<string>
    12 #include<cstring>
    13 #include<list>
    14 #include<queue>
    15 #include<stack>
    16 #include<map>
    17 #include<set>
    18 #include<algorithm>
    19 #define MAXN 2000
    20 using namespace std;
    21 int N,M,S,k;
    22 double dis[MAXN];
    23 double V;
    24 struct edge
    25 {
    26     int begin,end;
    27     double r,c;
    28 }Edge[MAXN];
    29 bool Bellman()
    30 {
    31     memset(dis,0,sizeof(dis));
    32     dis[S]=V;
    33     bool flag=0;
    34     for (int i=1;i<=N-1;i++)
    35     {
    36         flag=0;
    37         for (int j=1;j<=k;j++)
    38             if (dis[Edge[j].end]<(dis[Edge[j].begin]-Edge[j].c)*Edge[j].r)
    39             {
    40                 dis[Edge[j].end]=(dis[Edge[j].begin]-Edge[j].c)*Edge[j].r;
    41                 flag=1;
    42             }
    43         if (!flag) break;
    44     }
    45     //再次判断是否会增加,增加则表示出现了正权回路
    46     for (int j=1;j<=k;j++)
    47         if(dis[Edge[j].end]<(dis[Edge[j].begin]-Edge[j].c)*Edge[j].r)
    48             return 1;
    49     return 0;
    50 }
    51 int main()
    52 {
    53     while (scanf("%d%d%d%lf",&N,&M,&S,&V)!=EOF)
    54     {
    55         k=1;
    56         for (int i=1;i<=M;i++)
    57         {
    58             int a,b;
    59             double rab,rba,cab,cba;
    60             scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
    61             Edge[k].begin=a;
    62             Edge[k].end=b;
    63             Edge[k].r=rab,Edge[k].c=cab;
    64             k++;
    65             Edge[k].begin=b;
    66             Edge[k].end=a;
    67             Edge[k].r=rba,Edge[k].c=cba;
    68             k++;
    69         }
    70         k--;
    71         bool ok=Bellman();
    72         if (ok) cout<<"YES"<<endl;
    73         else cout<<"NO"<<endl;
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    工商银行在线支付接口
    Runtime-compiler和Runtime-only的区别:
    响应式网站技术精要总结
    ajax基础
    jquery选择器 笔记
    js数组去重的几种方法
    js中的call apply bind
    es6中的类
    bootstrap组件
    es6中的模块化输出
  • 原文地址:https://www.cnblogs.com/Enumz/p/4060428.html
Copyright © 2011-2022 走看看