Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路-程序员宅基地

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 R AB, C AB, R BA and C BA - 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<=10  3.  For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.  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 10 4. 

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

题目意思:有n种货币,货币之间按照汇率交换,当然还要花费一些手续费,货币交换是可以多次重复进行的,问有没有可能经过一系列的货币交换,开始的货币会增加?
当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。

解题思路:这道题可以抽象为图论中的题,将货币种类看为点,货币之间的交换看为有向边,想要货币的金额产生增加,那么必然要有正权回路,即在一条回路上能够一直松弛下去。该题的问题主要在于所给的参数很多,第一行给出了n种货币有m种交换方式,给你第s种货币有V的金额,对于m种的交换方式,从x到y需要汇率rate和手续费commission,从y到x也需要这两个参数。同时这里的松弛递推公式也要发生变化:
            if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
            {
                dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
            }
因为是需要增加的正权回路,所以如果小于就松弛。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct Edge
{
    int f;
    int t;
    double r;
    double c;
} edge[1010];
double dist[605];
int n,m,s,cnt;
double x;
int bellman_ford()
{
    int i,j;
    int flag;
    for(i=1; i<=n; i++)
    {
        dist[i]=0;
    }
    dist[s]=x;
    for(j=1; j<=n; j++)
    {
        flag=0;
        for(i=1; i<=cnt; i++)
        {
            if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
            {
                dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
                flag=1;
            }
        }
        if(flag==0)
        {
            break;
        }
    }
    return flag;
}
int main()
{
    int i,t;
    int u,v;
    double a1,a2,b1,b2;
    while(scanf("%d%d%d%lf",&n,&m,&s,&x)!=EOF)
    {
        cnt=1;
        while(m--)
        {
            scanf("%d%d%lf%lf%lf%lf",&u,&v,&a1,&b1,&a2,&b2);
            edge[cnt].f=u;
            edge[cnt].t=v;
            edge[cnt].r=a1;
            edge[cnt++].c=b1;
            edge[cnt].f=v;
            edge[cnt].t=u;
            edge[cnt].r=a2;
            edge[cnt++].c=b2;
        }
        if(bellman_ford())
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

 附上使用SPFA的代码

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxs = 1e3+200;
int n,m;
struct Edge
{
    int to;
    double rate;
    double com;
} ;
double dis[maxs];
int vis[maxs];
int cnt[maxs];///用来记录入队列次数
vector<Edge>maps[maxs];
void AddEdge(int u,int v,double r,double co)
{
    Edge t;
    t.to=v;
    t.rate=r;
    t.com=co;
    maps[u].push_back(t);
}
int SPFA(int s, double v)
{
    int i;
    memset(dis,0,sizeof(0));
    memset(vis,0,sizeof(0));
    memset(cnt,0,sizeof(0));
    queue<int>q;
    dis[s]=v;
    vis[s]=1;
    cnt[s]++;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(i=0; i<maps[u].size(); i++)
        {
            int to=maps[u][i].to;
            double com=maps[u][i].com;
            double rate=maps[u][i].rate;
            if(dis[to]<(dis[u]-com)*rate)
            {
                dis[to]=(dis[u]-com)*rate;
                if(!vis[to])
                {
                    vis[to]=1;
                    cnt[to]++;
                    if(cnt[to]>=n)
                    {
                        return 1;
                    }
                    q.push(to);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int s,i;
    double k;
    while(scanf("%d%d%d%lf",&n,&m,&s,&k)!=EOF)
    {
        int a,b;
        double c,d,e,f;
        while(m--)
        {
            scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
            AddEdge(a,b,c,d);
            AddEdge(b,a,e,f);
        }
        if(SPFA(s,k))
        {
            puts("YES");
        }
        else
        {
           puts("NO");
        }
    }
    return 0;
}

 


 


转载于:https://www.cnblogs.com/wkfvawl/p/10535437.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30487317/article/details/98868197

智能推荐

C++程序员应了解的那些事(68)非类型模板参数_包含非静态存储持续时间的变量不能用作非类型参数-程序员宅基地

文章浏览阅读1.4k次,点赞3次,收藏9次。模板除了定义类型参数,我们还可以在模板定义非类型参数。什么是非类型形参?顾名思义,就是表示一个固定类型的常量而不是一个类型。※ 固定类型是有局限的,只有整形,指针和引用才能作为非类型形参;※ 而且绑定到该形参的实参必须是常量表达式,即编译期就能确认结果。非类型形参的局限:1.浮点数不可以作为非类型形参,包括float,double。具体原因可能是历史因素,也许未来C++会支持浮点数;2.类不可以作为非类型形参;3.字符串不可以作为非类型形参;4.整形,可转化为整形的类型都可以作为形参,比如int_包含非静态存储持续时间的变量不能用作非类型参数

正规的IT外包公司的报价组成_软件劳务派遣报价-程序员宅基地

文章浏览阅读3k次。在IT驻场外包中,外包公司在派遣人员与用人单位之间到底从中抽了多少?_软件劳务派遣报价

ganglia安装-程序员宅基地

文章浏览阅读68次。我是参考 http://www.ibm.com/developerworks/cn/linux/l-ganglia-nagios-1/ 这篇文章搭建的ganglia,部分内容页引自这篇文章,与原文不同之处用红色标出,操作系统是CentOS 5.7 x86_64。安装 Ganglia先决条件假定您已经设置了 yum 库,安装先决条件在很大程度上应当十分简单。类...

Magics修复STL文件_magics导入零件 stp-程序员宅基地

文章浏览阅读7k次。Magics RP是比利时Materialise公司开发的、完全针对3D打印工序特征的软件,其目前最新版本为19.01。Magics为处理STL文件提供了理想的、完美的解决方案,具有功能强大、易用、高效等优点,是从事3D打印行业必不可少的软件。在3D打印行业,Magics常用于零件摆放、模型修复、添加支撑、切片等环节。  由于STL文件结构简单,没有几何拓扑_magics导入零件 stp

oracle 学习网站收集-程序员宅基地

文章浏览阅读1.7k次。《转载》Oracle官方站:Oracle中文官网metalink.oracle.comOracle官方知识库,需要付费帐号登陆tahiti.oracle.comsearch and download documentation for Oracle's server productsOracle11gR1Online DocumentationOracle10gR2 Online Docu

【毫米波雷达】毫米波雷达接收发射信号matlab仿真_毫米波雷达仿真-程序员宅基地

文章浏览阅读872次,点赞22次,收藏26次。毫米波雷达是一种利用毫米波段电磁波来探测目标的雷达系统。它具有体积小、重量轻、功耗低、分辨率高、抗干扰能力强等优点,广泛应用于汽车、航空、航天、军事等领域。毫米波雷达的工作原理是:雷达发射机发射毫米波电磁波,电磁波遇到目标后反射,反射波被雷达接收机接收,并根据反射波的强度、频率和相位等信息来确定目标的位置、速度和姿态。毫米波雷达的接收发射信号主要包括以下几个步骤:发射信号毫米波雷达发射机产生毫米波电磁波,并通过天线发射出去。发射信号的频率、功率和波形等参数由雷达系统的设计要求决定。信号传播。_毫米波雷达仿真

随便推点

C语言 在一维数组中找出值最小的元素,并将其与第一个元素的值对调_在一维数组中找出值最小的元素,并将其值与第一个元素的值对调。-程序员宅基地

文章浏览阅读9.6k次,点赞7次,收藏21次。因本人才疏学浅,见识浅薄,有不当之处望指正,谢谢!在一维数组中找出值最小的元素,并将其与第一个元素的值对调思路:每次比较过程中,若一个数比最小的数还要小。那它就是最小的数// 找最小,并和第一个元素的值互换#include &amp;lt;stdio.h&amp;gt;#define N 10int main(void){ int a[N],i,t,min =0; printf(&quot;input ..._在一维数组中找出值最小的元素,并将其值与第一个元素的值对调。

IDEA中快捷创建SpringBoot主启动类的方法的设置_idea本地启动spring配置主类-程序员宅基地

文章浏览阅读4.9k次,点赞4次,收藏11次。IDEA中快捷创建SpringBoot主启动类的方法的设置,自动同步同类名的参数_idea本地启动spring配置主类

Android 动态添加View 并设置id_android字符串动态生成view id-程序员宅基地

文章浏览阅读2.7w次,点赞14次,收藏40次。主页面布局(main_activity.xml) LinearLayout 里面加一个Button,注意这里的LinearLayout要有orientation&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;LinearLayout ="http://schemas.android.com/apk..._android字符串动态生成view id

[arcgis插件]尖锐角检查/批量处理工具-GIS程序猿_arcgis如何查尖锐角-程序员宅基地

文章浏览阅读459次。2、设置合并优先级。选择字段,设置优先级。无需优先级,可以吧文字清空,则会根据与地块有相同信息字段的值来合并。[arcgis插件]尖锐角检查/批量处理工具,支持arcgis10.2-10.8版本。7、仅仅检查选中的地块:先选中地块再执行流程。5、处理流程设置:1 处理,2 切割,3 合并。6、顺便检查选择检查狭长面、自相交、重复节点。4、存在尖锐角并且面积小于这个面积阈值,则无需切割,直接合并。可以选择shp数据、GDB或者MDB的矢量面图层。年度变更,又是尖锐角,死磕尖锐角,就不信搞不定它。_arcgis如何查尖锐角

例子:BlackBerry真正的后台运行程序,Task里面看不到的哦_黑莓手机guid-程序员宅基地

文章浏览阅读5k次。说明:1.BlackBerry_App_Descriptor.xml设置程序为Auto-run on startup,Do not display the application icon on the BlackBerry home screen2.手机开机后自动运行 BackgroundApplication3.主程序BackgroundApplication的main中,执行BackgroundThread.waitForSingleton().start();启动后台线程4.BackgroundTh_黑莓手机guid

oracle中查找执行效率低下的SQL_oracle 怎么抓取执行慢的sql-程序员宅基地

文章浏览阅读9.9k次。oracle中查找执行效率低下的SQLkt431128 发布于 9个月前,共有 0 条评论v$sqltext:存储的是完整的SQL,SQL被分割v$sqlarea:存储的SQL 和一些相关的信息,比如累计的执行次数,逻辑读,物理读等统计信息(统计)v$sql:内存共享SQL区域中已经解析的SQL语句。(即时) select opname, ta_oracle 怎么抓取执行慢的sql

推荐文章

热门文章

相关标签