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

智能推荐

python编码问题之encode、decode、codecs模块_python中encode在什么模块-程序员宅基地

文章浏览阅读2.1k次。原文链接先说说编解码问题编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。 Eg:str1.decode('gb2312') #将gb2312编码的字符串转换成unicode编码str2.encode('gb2312') #将unicode编码..._python中encode在什么模块

Java数据流-程序员宅基地

文章浏览阅读949次,点赞21次,收藏15次。本文介绍了Java中的数据输入流(DataInputStream)和数据输出流(DataOutputStream)的使用方法。

ie浏览器无法兼容的问题汇总_ie 浏览器 newdate-程序员宅基地

文章浏览阅读111次。ie无法兼容_ie 浏览器 newdate

想用K8s,还得先会Docker吗?其实完全没必要-程序员宅基地

文章浏览阅读239次。这篇文章把 Docker 和 K8s 的关系给大家做了一个解答,希望还在迟疑自己现有的知识储备能不能直接学 K8s 的,赶紧行动起来,K8s 是典型的入门有点难,后面越用越香。

ADI中文手册获取方法_adi 如何查看数据手册-程序员宅基地

文章浏览阅读561次。ADI中文手册获取方法_adi 如何查看数据手册

React 分页-程序员宅基地

文章浏览阅读1k次,点赞4次,收藏3次。React 获取接口数据实现分页效果以拼多多接口为例实现思路加载前 加载动画加载后 判断有内容的时候 无内容的时候用到的知识点1、动画效果(用在加载前,加载之后就隐藏或关闭,用开关效果即可)2、axios请求3、map渲染页面4、分页插件(antd)代码实现import React, { Component } from 'react';//引入axiosimport axios from 'axios';//引入antd插件import { Pagination }_react 分页

随便推点

关于使用CryPtopp库进行RSA签名与验签的一些说明_cryptopp 签名-程序员宅基地

文章浏览阅读449次,点赞9次,收藏7次。这个变量与验签过程中的SignatureVerificationFilter::PUT_MESSAGE这个宏是对应的,SignatureVerificationFilter::PUT_MESSAGE,如果在签名过程中putMessage设置为true,则在验签过程中需要添加SignatureVerificationFilter::PUT_MESSAGE。项目中使用到了CryPtopp库进行RSA签名与验签,但是在使用过程中反复提示无效的数字签名。否则就会出现文章开头出现的数字签名无效。_cryptopp 签名

新闻稿的写作格式_新闻稿时间应该放在什么位置-程序员宅基地

文章浏览阅读848次。新闻稿是新闻从业者经常使用的一种文体,它的格式与内容都有着一定的规范。本文将从新闻稿的格式和范文两个方面进行介绍,以帮助读者更好地了解新闻稿的写作_新闻稿时间应该放在什么位置

Java中的转换器设计模式_java转换器模式-程序员宅基地

文章浏览阅读1.7k次。Java中的转换器设计模式 在这篇文章中,我们将讨论 Java / J2EE项目中最常用的 Converter Design Pattern。由于Java8 功能不仅提供了相应类型之间的通用双向转换方式,而且还提供了转换相同类型对象集合的常用方法,从而将样板代码减少到绝对最小值。我们使用Java8 功能编写了..._java转换器模式

应用k8s入门-程序员宅基地

文章浏览阅读150次。1,kubectl run创建pods[root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1[root@master ~]# kubectl get podsNAME READY STATUS REST...

PAT菜鸡进化史_乙级_1003_1003 pat乙级 最优-程序员宅基地

文章浏览阅读128次。PAT菜鸡进化史_乙级_1003“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。得到“答案正确”的条件是: 1. 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符; 2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或..._1003 pat乙级 最优

CH340与Android串口通信_340串口小板 安卓给安卓发指令-程序员宅基地

文章浏览阅读5.6k次。CH340与Android串口通信为何要将CH340的ATD+Eclipse上的安卓工程移植到AndroidStudio移植的具体步骤CH340串口通信驱动函数通信过程中重难点还存在的问题为何要将CH340的ATD+Eclipse上的安卓工程移植到AndroidStudio为了在这个工程基础上进行改动,验证串口的数据和配置串口的参数,我首先在Eclipse上配置了安卓开发环境,注意在配置环境是..._340串口小板 安卓给安卓发指令