Currency Exchange(bellman-Ford)

【代码超详解】POJ 1860 Currency Exchange(Bellman-Ford 算法判环) 一、题目描述 二、算法分析说明与代码编写指导 建立图论模型,以 n 种货币为顶点,从 u 到 v 的有向边代表用货币 u 换货币 v。因为货币可以双向兑换,所以总共有 2m 条有向边。每条边要保存 u 到 v 的汇率 r 和手续费 c。 题目的意思是:问 Nick 最后能否使自己货币的面值比起始时更高。 Bellman-Ford 算法是求解单源最短路径的算法,用一个数组 d 代表定点(源点)到... 阅读详情

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
货币互相有转换的公式,问能不能通过转化,钱变得多一点。
不会做……看了网上的做法,才知道这个是bellman的模板题,其实题目就是想让我们找负环,在这里,负环指得是一个环,每走一次,钱都会增加,那么我们走无限次,再回去,一定是增加财富的。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
double d[105];
int leap[105],head[105];
struct Node
{
    int u,v,next;
    double r,c;
}node[210];
void add_edge(int u,int v,double r,double c,int tot)
{
    node[tot].u = u;node[tot].v = v;
    node[tot].r = r;node[tot].c = c;
    node[tot].next = head[u];
    head[u] = tot;
}
void relax(int u,int v,double r,double c)
{
    if(d[u] >= c)
    d[v] = max(d[v],(d[u] - c)*r);
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int n,m,s;
    double v;
    while(scanf("%d%d%d%lf",&n,&m,&s,&v) != EOF)
    {
        for(int i = 1;i <= n;i++)head[i] = -1;
        for(int i = 1;i <= m;i++)
        {
            int from,to;
            double r,c;
            scanf("%d%d%lf%lf",&from,&to,&r,&c);
            add_edge(from,to,r,c,2*i - 1);
            scanf("%lf%lf",&r,&c);
            add_edge(to,from,r,c,2*i);
        }
        for(int i = 1;i <= n;i++)d[i] = 0;
        for(int i = 1;i <= n;i++)leap[i] = 0;
        d[s] = v;
        for(int i = 1;i < n;i++)
        {
            for(int j = 1;j <= m*2;j++)
                relax(node[j].u,node[j].v,node[j].r,node[j].c);
        }
        bool flag = 0;
        for(int i = 1;i <= m*2;i++)
        {
            int from = node[i].u;
            int to = node[i].v;
            if(d[from] >= node[i].c)
            {
                if((d[from] - node[i].c)*node[i].r > d[to])
                {
                    flag = 1;break;
                }
            }
        }
        if(flag)printf("YES\n");
        else printf("NO\n");

    }
    return 0;
}
【Benchmark 性能测试全指南:从核心原理到 CINEBENCH 实战解析】 本文深度拆解基准测试(Benchmark)的底层逻辑,详解数据集、工作负载、度量指标三大核心要素,分步演示测试流程与统计分析要点。结合 CINEBENCH R23 实战案例,揭秘 CPU 性能评估方法论,助你掌握系统性能量化与优化的核心技能。 阅读详情

相关推荐

西门子200SMART PID温控实战:从配置到避坑(附加热棒控制案例)

本文详细介绍了西门子200SMART PLC在PID温控系统中的实战应用,涵盖硬件配置、PID参数整定及工程避坑技巧。通过加热棒控制案例,重点解析了信号处理、固态继电器选型、断电保持和无扰切换等关键问题,为工业自动化领域的温度控制提供可靠解决方案。

weixin_28727975的博客 356

poj1860解题报告(两种解法)

给定一些站点可以交换货币,判断持有某一种货币的人是否可以增值财产.解法1:基本思路是寻找一个正环,包含出发点s.没有用Bellman_ford算法,采用的是深度搜索优先(DFS),当一个节点被访问过之后,再次访问,就说明找到了一条回路.这时候需要判断找到的回路是不是正的.比较当前节点的货币数量与出发时节点的货币数量(一个节点可能有很多货币状态,因为可以通过不同的途径到达该节点,所以需要利用一个栈把状

pyb的博客 874

卫星动力学仿真模型开发之AI初试

以下是通过AI生成的一个简单的C语言仿真模型,用于模拟卫星的姿态、轨道动力学和运动学。该模型基于基本的物理公式,假设卫星在近地轨道上运行,并忽略了一些复杂的扰动因素(如大气阻力、太阳辐射压等)。姿态动力学描述了卫星绕其质心的旋转运动。轨道动力学描述了卫星在引力作用下的运动。姿态运动学描述了卫星姿态随时间的变化。

xspace的博客 569

poj1860(Bellman-Ford算法)

题意:首先给出四个数字:n–货币数量,M–交换点数,S–货币尼克的数量,V–他拥有的货币单位的数量; 后面的M行给出每个兑换点可以用6个数字来描述:整数A和B——它兑换的货币数量,以及实际RAB、CAB、RBA和CBA——A兑换B和B兑换A时的汇率和佣金。 思路:采用Bellman-Ford算法: 1.初始化时将起点 s 到各个顶点 v 的距离 dist(s->v) 赋值为 ∞,dist(s->s) 赋值为 0; 2.后续进⾏最多 n-1 次遍历操作 (n 为顶点个数), 对所有的边进⾏松弛操作

Keep_Trying_Go的博客 1068

ANSYS MAXWELL内置式永磁同步电机(IPMSM)转矩最大时输入电流相位的确定

1.在转子转速为零时看一个周期内的转矩变化

qq_41752507的博客 6516

【2026-Spark实验一-Windows中安装Spark环境】

本文详细介绍了在Windows系统中配置Spark 3.5.8开发环境的完整流程。主要内容包括:安装JDK 8并配置环境变量,下载并解压Spark 3.5.8预编译版,设置SPARK_HOME系统变量,配置Hadoop的winutils工具解决Windows兼容性问题。通过spark-shell和SparkPi示例程序验证环境配置成功,确保Spark能够正常运行分布式计算任务。文中特别强调了路径命名规范、环境变量设置顺序等关键细节,提供了完整的配置截图和验证方法,帮助开发者快速搭建稳定的Spark学习环境。

pblh123的专栏 1586

POJ 刷题系列:1860. Currency Exchange

POJ 刷题系列:1860. Currency Exchange传送门:1860. Currency Exchange题意: N种货币,任意货币之间可以相互转换,转换需要手续费commission,假设货币A当前金额为money,转换公式如下:(money - commission) * rate,现给出任意货币之间的转换手续和转换率,问从S点出发,是否存在一条路径使得回到S时,能够增值。 思路

Demon-初来驾到 2774

掌控板玩板指南1:初识掌控板、扩展板、Mind+编辑器

掌控板、I/O扩展板入门

好乐无荒之地 1万+

【SPFA算法】POJ1860

【SPFA算法】POJ1860

m0_71009069的博客 252

Virtual Hard Disk version 2 (VHDX) 文件格式详解

VHDX 镜像格式详解 0. References windows github 1. Overview Microsoft 虚拟化产品中常用的映像格式是虚拟硬盘版本 2 (VHDX) 映像格式。该规范基于可用文档,并通过分析测试数据进行了增强。 VHDX 镜像格式种类 固定硬盘镜像 动态硬盘镜像 差异硬盘镜像 VHDX 镜像文件由以下元素构成: file (type) identifier 2x image headers 2x region tables log or metadata journ

圣三一 2694

【BlueDroid】蓝牙音乐协议分析之A2DP和AVRCP连接流程(超详细)

AVCTP协议描述了蓝牙设备间Audio/Video的控制信号交换的格式和机制,他是一个总体的协议,具体的控制信息由其指定的协议(AVRCP)实现,AVCTP协议本身只指定控制Command和Response的总体的格式;这个过程中对应了Event中的btavrcp_play_status_changed_callback和btavrcp_play_position_changed_callback;上述为AVCTP-Control建立L2CAP连接的Command,对应H4的Sent (0x00);

风云说通信 7313

POJ 1860(最短路之Bellman-Ford)

题目链接:http://poj.org/problem?id=1860 题意:首先给出四个数n,m,s,v,分别表示n种货币,m个兑换关系,s源货币,源货币的本金v 然后给出m行,每行给出6个数beg,end,r1,c1,r2,c2,分别表示beg和end货币兑换的比率和手续费,end和beg货币兑换的比率和手续费 。问能否通过货币兑换使手里的源货币依然是第s中货币,并且本金增多。 思路:

To be excellent! 902

YOLOv8改进 | 特殊场景检测篇 | 最新的多尺度特征提取DICAM(全网独家首发,增强水下图像的质量)

本文给大家带来的最新改进机制是多尺度特征提取和通道注意力机制DICAM,深度Inception和通道注意力模块(DICAM)主要用于用于增强水下图像的质量、对比度和色偏。所提出的DICAM模型考虑了水下图像的比例退化和不均匀色偏,从而提高图像的质量。通过在两个公开的水下图像增强数据集上的广泛实验,验证了提出的模型在全参考和无参考图像质量评估指标方面。欢迎大家订阅我的专栏一起学习YOLO,购买专栏读者联系读者入群获取进阶项目文件!

Snu77的博客 2216

【POJ 1860】Currency Exchange(Bellman_Ford)

DescriptionSeveral 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.

Ice_Confuse 558

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

POJ 1860 Currency Exchange [bellman_ford]

传送门:http://poj.org/problem?id=1860 题目: Currency Exchange Time Limit:1000MS   Memory Limit:30000KB   64bit IO Format:%I64d & %I64u SubmitStatus Description Sev

code_or_code 606

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

USB设备识别与驱动加载:从VID/PID到代码43排查实战

USB设备识别依赖于VID和PID,主机通过设备描述符获取这些信息并匹配驱动。枚举过程中,主机读取描述符、分配地址、加载驱动,任何环节出错都可能导致代码43错误。理解VID/PID和描述符结构,能帮助开发者快速定位驱动安装失败、设备无法识别等问题。在嵌入式开发和硬件调试中,掌握USB抓包和描述符分析技术,可有效解决枚举失败、驱动不匹配等常见故障,提升开发效率。

weixin_34175509的博客 330

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Currency Exchange 反向Bellman-Ford

Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Problem Description Several currency exchang

停不下的脚步 569

最短路(Bellman_Ford) POJ 1860 Currency Exchange

题目传送门 1 /* 2 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 3 详细解释:http://blog.csdn.net/lyy289065406/article/details/6645778 4 */ 5 #include <cstdio> 6 #include <iostream&...

weixin_30881367的博客 70

Currency Exchange——最短路Bellman-Ford算法

【题目描述】 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. Th...

T_T233333333的博客 431

Bellman-Ford-POJ-1860-Currency Exchange

Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23595 Accepted: 8542 DescriptionSeveral currency exchange points are working in our city. Let us suppose that

懒鬼的私人泳池 626

POJ1860 Currency Exchange【bellman-Ford算法】

http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs ex

*乘号的笔记本 378

poj 1860 Currency Exchange(bellman-ford)

货币兑换,使得货币最后再兑换回S的时候Nick的总资产是增加的,这就要使兑换货币的路径有一个正的回路,才可以使Nick的总资产是增加的。用bellman-ford找回路,回路存在,则YES,否则NO。http://blog.csdn.net/yslcl12345/article/details/50574213,这个题解用的floyd做的,题目翻译的挺好的#include <iostream> #i

梦幻的蔷薇色 420

POJ 1860:Currency Exchange(Bellman_Ford算法)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12217   Accepted: 4120 Description Several currency exchange points are working in our ci

WU.GJ 2971

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit:1000MS Memory Limit:30000K Total Submissions:22405 Accepted:8095 Description Several currency exchange points are working in our c...

155

Currency Exchange(Bellman_ford算法)

原题链接 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27166 Accepted: 10058 DescriptionSeveral currency exchange points are working in our city. Let us suppo

HowieMen的博客 549

poj 1860 Currency Exchange(Bellman_ford)

最短路问题,但是刚开始的时候没什么想法,因为看到题目中要求回到源点,但是最短路都是求一个点到另一个点的最短路,想到要处理环,就有点头疼,因为对最短路理解的不透彻,又上网认真的学了学求最短路的各种方法。 Dijkskra:贪心的思想,从源点开始,先查看所有与源点相连的点,找出一条最短的路,然后从这个点重复上一个过程,直到找到目标点。要求图上的权值为非负数,时间复杂度为O(n2)。 Fl...

B218707的博客 379
上一篇: Silver Cow Party(边取反最短路)
下一篇: Wormholes(bellman-Ford)
Techay
博客等级 码龄11年 11粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值