最短路练习9/poj/2240 Arbitrage;正环:floyd或spfa解法;

本文介绍了一种通过货币兑换实现利润最大化的算法问题。利用Floyd和SPFA算法判断是否存在获利的货币兑换路径。


题目链接:http://poj.org/problem?id=2240
Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22913 Accepted: 9694

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
题意:货币兑换,货币兑换货币,能否增值。两种解法,因为不是单源,所以floyd更简单,spfa比较麻烦;

坑点就有一个,后台数据可能会出现1美元兑换2美元这种情况;

方法floyd(处理多源更方便:AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 +10;
int n,m,ans;
double x,mp[50][50];
string a,b;
map<string,int>ma;
bool floyd()
{
    for(int k=0;k<n;k++)
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            if(mp[i][k]*mp[k][j]>mp[i][j])
                mp[i][j]=mp[i][k]*mp[k][j];
            if(mp[i][i]>1.0)
                return 1;
        }
    return 0;
}
int main()
{
    int fla=1;
    while(~scanf("%d",&n)&&n)
    {
        ma.clear();
        memset(mp,0,sizeof(mp));
        for(int i=0; i<n; i++)
            mp[i][i]=1.0;//初始化放在cin前面。我自己打的代码就一直wa在这。
        for(int i=0; i<n; i++)
        {
            cin>>a;
            ma[a]=i;
        }
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            cin>>a>>x>>b;
            if(mp[ma[a]][ma[b]]<x)//这道题没有这个坑点,这个if()可以不要。
            mp[ma[a]][ma[b]]=x;
        }
        if(floyd())
            printf("Case %d: Yes\n",fla++);
        else
            printf("Case %d: No\n",fla++);
    }
    return 0;
}

方法spfa(处理单源更方便)(虽然麻烦但是还是要锻炼一下代码能力的):AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 +10;
int n,m,ans;
double x;
double mp[50][50];
double dis[50];
int vis[50];
int cnt[50];
string a,b;
map<string,int>ma;
queue<double>q;
bool spfa(int y)
{
    while(!q.empty())
        q.pop();
    memset(cnt,0,sizeof(cnt));
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    dis[y]=1.0;
    double v=1.0;
    q.push(y);
    vis[y]=1;
    cnt[y]++;
    while(!q.empty())
    {
        int f=q.front();
        q.pop();
        vis[f]=0;
        for(int i=0; i<n; i++)
            if(dis[i]+eps<dis[f]*mp[f][i])
            {
                dis[i]=dis[f]*mp[f][i];
                if(dis[y]>v+eps)
                    return 1;
                if(!vis[i])
                {
                    vis[i]=1;
                    q.push(i);
                    cnt[i]++;
                    if(cnt[i]>n)
                     return 0;
                }
            }
    }
    return 0;
}
int main()
{
    int fla=1;
    while(~scanf("%d",&n)&&n)
    {
        ma.clear();
        memset(mp,0,sizeof(mp));
        for(int i=0; i<n; i++)
            mp[i][i]=1.0;
        for(int i=0; i<n; i++)
        {
            cin>>a;
            ma[a]=i;
        }
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            cin>>a>>x>>b;
            mp[ma[a]][ma[b]]=x;
        }
            ans=0;
        for(int i=0; i<n; i++)
            if(spfa(i))//spfa是处理单源的,加一个i=0到n就成了多源的;
            {
                ans=1;
                break;
            }
        if(ans) printf("Case %d: Yes\n",fla++);
        else printf("Case %d: No\n",fla++);
    }
    return 0;
}












内容概要:本文围绕“基于多面体聚合与闵可夫斯基和的电动汽车可调能力评估研究”展开,系统提出一种基于多面体建模与几何运算的聚合方法,用于量化大规模电动汽车集群的可调节能力。通过构建单车充电可行域的凸多面体表示,并利用闵可夫斯基和实现群体调控潜力的数学聚合,形成统一的等效灵活资源集合,从而精确刻画电动汽车集群在时间与功率维度上的整体调节边界。该方法结合Matlab代码实现,支持对聚合结果的可视化呈现与边界分析,为电力系统中的需求响应、虚拟电厂运营及分布式能源调度提供了高精度建模工具。研究强调科研应兼顾严谨逻辑与创新思维,倡导借助YALMIP等优化工具提升建模效率与求解可靠性。; 适合人群:具备电力系统分析、凸优化理论运筹学背景,从事新能源并网、电动汽车调度、综合能源系统等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握多面体建模与闵可夫斯基和在电力系统灵活性聚合中的数学原理与实现方法;②学习利用Matlab完成电动汽车集群可调能力的建模、聚合与可视化分析;③应用于虚拟电厂资源聚合、需求响应潜力评估、配电网柔性负荷调控等实际场景的建模与优化决策。; 阅读建议:建议读者结合文中Matlab代码逐模块复现算法流程,重点理解多面体定义、约束处理及闵可夫斯基和的近似计算实现,同时参考提供的YALMIP工具包资源,深入掌握优化建模技巧,以全面提升对复杂系统聚合建模的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值