Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6360    Accepted Submission(s):
2939

Problem 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 file 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
 
Recommend
Eddy   |   We have carefully selected several similar
problems for you:  1142 1162 1385 1301 1596 
 
最短路的变形,由于数据只到30,所以可以采用floyd算法,不过需要注意的是,这里是求最大的倍率。
 
题意:题目大意就是给了你各种货币之间的兑换关系,问你是否存在1个单元的某货币经过一个回路的兑换后>=1个单元( 有利润 )。
 
附上代码:
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #define M 35
  6. using namespace std;
  7. double map[M][M];
  8. int n;
  9.  
  10. void floyd() //利用floyd算法计算最大赔率
  11. {
  12. int k,i,j;
  13. for(k=; k<=n; k++)
  14. for(i=; i<=n; i++)
  15. for(j=; j<=n; j++)
  16. if(map[i][j]<map[i][k]*map[k][j])
  17. map[i][j]=map[i][k]*map[k][j];
  18. }
  19.  
  20. int main()
  21. {
  22. int m,i,j,w=;
  23. char s[M],str[M][M];
  24. while(~scanf("%d",&n)&&n)
  25. {
  26. for(i=; i<=n; i++)
  27. scanf("%s",str[i]);
  28. for(i=; i<=n; i++)
  29. for(j=; j<=n; j++)
  30. {
  31. if(i==j) map[i][j]=; //因为是找最大的汇率,因此初始时本身转本身为1,其他转化为0
  32. else map[i][j]=;
  33. }
  34. scanf("%d",&m);
  35. int a,b;
  36. double c;
  37. for(i=; i<=m; i++)
  38. {
  39. scanf("%s",s);
  40. for(a=; a<=n; a++) //将其转化为map数组记录
  41. if(!strcmp(s,str[a]))
  42. break;
  43. scanf("%lf",&c);
  44. scanf("%s",s);
  45. for(b=; b<=n; b++)
  46. if(!strcmp(s,str[b]))
  47. break;
  48. map[a][b]=c;
  49. }
  50. floyd();
  51. cout<<"Case "<<w++<<": ";
  52. if(map[][]>)
  53. cout<<"Yes"<<endl;
  54. else
  55. cout<<"No"<<endl;
  56. }
  57. return ;
  58. }

邻接表:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #define N 35
  6. #define M 35*35*10
  7. #define INF 0x3f3f3f3f
  8. using namespace std;
  9. struct Edge
  10. {
  11. int from,to;
  12. double val;
  13. int next;
  14. } edge[M*];
  15. int n,m,tol,s,t,fail;
  16. double dis[N];
  17. bool vis[N];
  18. int head[M*];
  19.  
  20. void init()
  21. {
  22. tol=;
  23. memset(head,-,sizeof(head));
  24. }
  25.  
  26. void addEdge(int u,int v,double w)
  27. {
  28. edge[tol].from=u;
  29. edge[tol].to=v;
  30. edge[tol].val=w;
  31. edge[tol].next=head[u];
  32. head[u]=tol++;
  33. }
  34.  
  35. void getmap()
  36. {
  37. char str[N][N];
  38. char s[N];
  39. for(int i=; i<=n; i++)
  40. scanf("%s",str[i]);
  41. int a,b;
  42. double c;
  43. scanf("%d",&m);
  44. while(m--)
  45. {
  46. scanf("%s",s);
  47. for(a=; a<=n; a++)
  48. if(!strcmp(s,str[a]))
  49. break;
  50. scanf("%lf",&c);
  51. scanf("%s",s);
  52. for(b=; b<=n; b++)
  53. if(!strcmp(s,str[b]))
  54. break;
  55. addEdge(a,b,c);
  56. }
  57. memset(vis,false,sizeof(vis));
  58. memset(dis,,sizeof(dis));
  59. }
  60.  
  61. void spfa()
  62. {
  63. queue<int>q;
  64. q.push();
  65. dis[]=1.0;
  66. vis[]=true;
  67. while(!q.empty())
  68. {
  69. int u=q.front();
  70. q.pop();
  71. vis[u]=false;
  72. for(int i=head[u]; i!=-; i=edge[i].next)
  73. {
  74. int v=edge[i].to;
  75. if(dis[v]<dis[u]*edge[i].val)
  76. {
  77. dis[v]=dis[u]*edge[i].val;
  78. if(!vis[v])
  79. {
  80. vis[v]=true;
  81. q.push(v);
  82. }
  83. if(dis[]>)
  84. {
  85. fail=;
  86. return;
  87. }
  88.  
  89. }
  90. }
  91. }
  92.  
  93. }
  94.  
  95. int main()
  96. {
  97.  
  98. int i,j,T=;
  99. while(~scanf("%d",&n)&&n)
  100. {
  101. init();
  102. getmap();
  103. printf("Case %d: ",T++);
  104. fail=;
  105. spfa();
  106. if(fail)
  107. printf("Yes\n");
  108. else
  109. printf("No\n");
  110. }
  111. return ;
  112. }

hdu 1217 Arbitrage(佛洛依德)的更多相关文章

  1. 佛洛依德 c++ 最短路径算法

    //20142880 唐炳辉 石家庄铁道大学 #include<iostream> #include<string> using namespace std; #define ...

  2. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  3. HDU 1217 Arbitrage (Floyd)

    Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...

  4. hdu 1217 Arbitrage (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217 /************************************************* ...

  5. HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...

  6. hdu 1217 Arbitrage (spfa算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...

  7. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

  8. hdu 1217 Arbitrage

    Flody多源最短路 #include<cstdio> #include<cstring> #include<string> #include<cmath&g ...

  9. HDU 1217 Arbitrage(Floyd的应用)

    给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include< ...

随机推荐

  1. 阿里云MaxCompute 2019-7月刊

    您好,MaxCompute 2019.7月刊为您带来7月产品.技术最新动态,欢迎阅读. 导读 [发布]7月产品重要发布 [资讯]7月重要资讯 [文档]7月重要文档更新推荐 [干货]7月精选技术文章推荐 ...

  2. 阿里云DMS发布数据库网关服务: 打通网络限制 开启数据库统一管理的万能钥匙

    概述 阿里云数据管理DMS在云端可提供专业的数据库服务,除对标本地数据库软件的基础功能外,还包含性能诊断.数据追踪.跨实例SQL查询(含异构数据库类型之间)等专业性功能,同时提供审计安全和企业级数据库 ...

  3. 模拟3题解 T3建造游乐园

    T3建造游乐园 这题的关键是推式子 i个点中,有g[i]个方案是度为偶数但不一定连通那么就要减去不合法的设已有j个合法,其个数为f[j],剩下i-j个的方案数是g[i-j]选出来一个固定的点在合法的j ...

  4. WordPress使用自定义文章类型实现任意模板的方法和怎么做邮件回复

    主要就是使用了register_post_type 函数. 1.创建插件目录 新建一个文件夹用来存放插件文件,这里我就命名这个文件夹为myMood 2.创php代码文件 在刚才创建的文件夹里面新建一个 ...

  5. Servlet表单处理

    HttpServletRequest   继承ServletRequest  HttpServletRequest生命周期: 一个HttpServletRequest对象在用户向web服务器发送请求时 ...

  6. 一键制作启动elasticsearch和kibana启动的脚本可执行程序

    1.测试环境 测试环境: . windows10专业版 . elasticsearch6.5.4 . kibana6.5.4 2.启动的脚本run.py import os import time i ...

  7. No.2 Verilog 模块和描述风格

    2-1 模块 Verilog语言基本的描述单元----模块,模块是用来描述某个设计的功能或结构,以及它与其它外部模块进行通信的端口. module module_name(port_list); De ...

  8. prepareStatement设置参数,mysql数据出现中文‘?’的情景与解决方式

    在prepareStatement中传入中文的参数,mysql数据库中出现“?”号 try { Class.forName("com.mysql.jdbc.Driver"); co ...

  9. Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离

    给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(Tr ...

  10. windows.open window.location.href的用法和区别

    window.location.href  只能在当前页面打开,不能用新窗口打开 windows.open("URL","窗口名称","窗口外观设定& ...