Arbitrage(最短路 + Bellman Ford + map)

本文介绍了一种使用贝尔曼-福特算法来检测是否存在货币套利机会的方法。通过构建图模型并利用该算法查找负权重环,以此判断是否可以通过一系列货币转换获得利润。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Arbitrage
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 14163  Accepted: 5957

Description

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. 

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not. 

Input

The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".

Sample Input

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

Sample Output

Case 1: Yes
Case 2: No

 

    题意:

    给出 N(1 ~ 30),代表有 N 种钱币,后给出这 N 种钱币。后给出 M 条单向关系,代表前者兑换为后者的利率。问是否能使某种钱币升值。

 

    思路:

    最短路。同样也是Bellman ford,判断正环,利率可以不断相乘来累加,找到正环就能不断累加钱币,每种钱币都用字符串表示,转化为map对应匹配即可。

 

    AC:

#include <map>
#include <string.h>
#include <string>
#include <cstdio>
using namespace std;

typedef struct {
    int u,v;
    double w;
}node;

node edg[1000];
int ind,n;
double d[35];

int Bellman_ford() {
    memset(d,0,sizeof(d));
    d[1] = 1;
    for(int i = 1;i < n;i++)
        for(int j = 0;j < ind;j++) {
        int x = edg[j].u,y = edg[j].v;
        double r = edg[j].w;
        if(d[y] < d[x] * r) d[y] = d[x] * r;
    }

    for(int i = 0;i < ind;i++) {
        int x = edg[i].u,y = edg[i].v;
        double r = edg[i].w;
        if(d[y] < d[x] * r) return 1;
    }

    return 0;
}

int main() {
    //freopen("test.in","r",stdin);
    int time = 0;
    map<string,int> m;
    while(~scanf("%d",&n) && n) {
        int k;
        ind = 0;
        time++;
        for(int i = 1;i <= n;i++) {
            char str[100];
            scanf("%s",str);
            m[str] = i;
        }

        scanf("%d",&k);
        while(k--) {
            char st[100],en[100];
            double val;
            scanf("%s%lf%s",st,&val,en);
            edg[ind].u = m[st];
            edg[ind].v = m[en];
            edg[ind].w = val;
            ind++;
        }

        printf("Case %d: ",time);
        if(Bellman_ford())  puts("Yes");
        else    puts("No");
    }
    return 0;
}

 

 

资源下载链接为: https://pan.quark.cn/s/3d8e22c21839 随着 Web UI 框架(如 EasyUI、JqueryUI、Ext、DWZ 等)的不断发展与成熟,系统界面的统一化设计逐渐成为可能,同时代码生成器也能够生成符合统一规范的界面。在这种背景下,“代码生成 + 手工合并”的半智能开发模式正逐渐成为新的开发趋势。通过代码生成器,单表数据模型以及一对多数据模型的增删改查功能可以被直接生成并投入使用,这能够有效节省大约 80% 的开发工作量,从而显著提升开发效率。 JEECG(J2EE Code Generation)是一款基于代码生成器的智能开发平台。它引领了一种全新的开发模式,即从在线编码(Online Coding)到代码生成器生成代码,再到手工合并(Merge)的智能开发流程。该平台能够帮助开发者解决 Java 项目中大约 90% 的重复性工作,让开发者可以将更多的精力集中在业务逻辑的实现上。它不仅能够快速提高开发效率,帮助公司节省大量的人力成本,同时也保持了开发的灵活性。 JEECG 的核心宗旨是:对于简单的功能,可以通过在线编码配置来实现;对于复杂的功能,则利用代码生成器生成代码后,再进行手工合并;对于复杂的流程业务,采用表单自定义的方式进行处理,而业务流程则通过工作流来实现,并且可以扩展出任务接口,供开发者编写具体的业务逻辑。通过这种方式,JEECG 实现了流程任务节点和任务接口的灵活配置,既保证了开发的高效性,又兼顾了项目的灵活性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值