I - Arbitrage(正权回路+map映射)

本文介绍了一种利用货币兑换率差异进行套利的算法。通过分析不同货币间的兑换比率,程序能够判断是否存在获利机会。文章提供了两个实现示例,分别采用Bellman-Ford算法和SPFA算法来解决此问题。

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

//普通的方法:
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
    int a,b;
    double c;
    char s[31];
} p[1001], e[1001];
int n,k;
int Bellman_Ford(int s,double v)//使用Bellman_Ford去判断是否存在一条正权边可以无限松弛
{
    int i,j;
    double dis[505];//dis[i] i是货币编号 dis是该种货币的价值
    memset(dis,0,sizeof(dis));
    dis[s]=v;
    for(i=1; i<=n-1; i++)
    {
        for(j=0; j<k; j++)
        {
            if(dis[e[j].b]<(dis[e[j].a]*e[j].c*1.0))
                dis[e[j].b]=dis[e[j].a]*e[j].c*1.0;
        }
    }
    for(j=0; j<k; j++)//判断正权回路
    {
        if(dis[e[j].b]<(dis[e[j].a]*e[j].c*1.0))
            return 1;
    }
    return 0;
}
int main()
{
    int cnt=0,i,j,m,a,b;
    double c;
    char st1[31],st2[31];
    while(scanf("%d",&n)!=EOF&&n)
    {
        cnt++;
        memset(p,0,sizeof(p));
        memset(e,0,sizeof(e));
        for(i=0; i<n; i++)
        {
            //getchar();
            scanf("%s",p[i].s);
        }
        scanf("%d",&m);k=0;
        for(i=0; i<m; i++)
        {
            getchar();
            scanf("%s %lf %s",st1,&c,st2);
            for(j=0; j<n; j++)
            {
                if(strcmp(st1,p[j].s)==0) a=j;
                if(strcmp(st2,p[j].s)==0) b=j;
            }
            e[k].a=a;
            e[k].b=b;
            e[k++].c=c;
        }
        printf("Case %d: ",cnt);
        if(Bellman_Ford(0,1.0)) printf("Yes\n");
        else printf("No\n");
    }

    return 0;
}
//用map函数讲 string 映射成 int
#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <string>/***/
#include <queue>
#include <map>
#include <math.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
    int to,next;
    double w;
} edge[1001];
int head[1001],cnt;
void add(int u,int v,double w)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int n,m;
int spfa(int s)
{
    queue<int>quee;
    int u,i,v;
    double dis[101];
    int out[101];
    int vis[101];
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(out,0,sizeof(out));
    quee.push(s);
    vis[s]=1;
    dis[s]=1;
    while(!quee.empty())
    {
        u=quee.front();
        quee.pop();
        vis[u]=0;
        for(i=head[u]; i!=-1; i=edge[i].next)
        {
            v=edge[i].to;
            if(dis[v]<dis[u]*edge[i].w*1.0)
            {
                dis[v]=dis[u]*edge[i].w*1.0;
                out[v]++;
                if(out[v]>n) return 0;//松弛超过n次存在正权回路
                if(!vis[v])
                {
                    quee.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    return 1;
}
int main()
{
    int ct=0,i;
    double c;
    string st1,st2,st;
    while(scanf("%d",&n)!=EOF&&n)
    {
        ct++;
        map<string,int> ys;/***/
        for(i=0; i<n; i++)
        {
            cin >> st; /***/
            ys[st]=i;//映射成数字编号
        }
        scanf("%d",&m);
        cnt=0;
        memset(head,-1,sizeof(head));
        while(m--)
        {
            cin >> st1 >>c >>st2;
            add(ys[st1],ys[st2],c);
        }
        printf("Case %d: ",ct);
        if(spfa(0)) printf("No\n");
        else printf("Yes\n");
    }
    return 0;
}

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值