设为首页 收藏本站
查看: 1012|回复: 0

[经验分享] poj2240

[复制链接]

尚未签到

发表于 2017-7-4 21:34:14 | 显示全部楼层 |阅读模式
Arbitrage


Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20021 Accepted: 8497

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

Source


Ulm Local 1996  题意:求自身到自身的最大转换率。
   最简单的方法就是floryd算法变形,求最大路径后,求最大环,看它是否满足条件。
每一个结点都必须有到自身的环(不甚清楚原因)。
注意:该double的地方一定不能为int,切记!!!



#include<iostream>
#include<cstdio>
#include<map>
#include<string>
#include<cstring>
using namespace std;
const int inf=1e6;      //无限大
int n;//货币种类
int m;//兑换方式
map<string,int>f;     //建立一个 使字符串与整数有一一对应关系 的容器f,以便利用邻接矩阵存储数据
double rate;
char str[50],str1[50],str2[50];
double dist[31][31];
int main()
{
int cases=0;
for(;;)
{
memset(dist,inf,sizeof(dist));
cin>>n;
if(!n) return 0;
for(int i=1;i<=n;i++){
cin>>str;
f[str]=i;          //将输入的货币从1到n依次编号
dist=1;        //到自身的转换率默认为1,但通过floyd可能会被改变     
}//有向图的顶点(一般)存在环
cin>>m;
for(int i=1;i<=m;i++){
cin>>str1>>rate>>str2;
dist[f[str1]][f[str2]]=rate;      //构造图
        }
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dist[j]<dist[k]*dist[k][j])//变形的最大路径,变"+"为"*"
dist[j]=dist[k]*dist[k][j];
int flag=0;
for(int i=1;i<=n;i++)
if(dist>1){
flag=1;break;
}
if(flag)
cout<<"Case "<<++cases<<": Yes"<<endl;
else
cout<<"Case "<<++cases<<": No"<<endl;
}
return 0;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390780-1-1.html 上篇帖子: RabbitMQ安装和使用(和Spring集成) 下篇帖子: j2ee分布式缓存同步实现方案dlcache v1.0.0
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表