点击打开链接

Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13434   Accepted: 5657

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

  1. 3
  2. USDollar
  3. BritishPound
  4. FrenchFranc
  5. 3
  6. USDollar 0.5 BritishPound
  7. BritishPound 10.0 FrenchFranc
  8. FrenchFranc 0.21 USDollar
  9.  
  10. 3
  11. USDollar
  12. BritishPound
  13. FrenchFranc
  14. 6
  15. USDollar 0.5 BritishPound
  16. USDollar 4.9 FrenchFranc
  17. BritishPound 10.0 FrenchFranc
  18. BritishPound 1.99 USDollar
  19. FrenchFranc 0.09 BritishPound
  20. FrenchFranc 0.19 USDollar
  21.  
  22. 0

Sample Output

  1. Case 1: Yes
  2. Case 2: No

题目大意:给你几种货币的名字,还有货币之间的汇率,问你是否通过兑换使货币数量增加

bellmanford或者spfa计算是否有负圈回路

  1. #include<stdio.h>
  2. #include<string>
  3. #include<map>
  4. #include<iostream>
  5. using namespace std;
  6. double g[31][31];
  7. double dis[31];
  8. int n;
  9. bool bellman()
  10. {
  11. int i, j, k;
  12. for(i = 0; i < 31; i++)
  13. dis[i] = 1;
  14. for(i = 1; i < n; i++)
  15. {
  16. for(j = 1; j <= n; j++)
  17. {
  18. for(k = 1; k <= n; k++)
  19. {
  20. if(dis[k] < dis[j] * g[j][k])
  21. dis[k] = dis[j] * g[j][k];
  22. }
  23. }
  24. }
  25. for(j = 1; j <= n; j++)
  26. {
  27. for(k = 1; k <= n; k++)
  28. {
  29. if(dis[k] < dis[j] * g[j][k])
  30. return 0;
  31. }
  32. }
  33. return 1;
  34. }
  35. int main()
  36. {
  37. int m, t = 1;
  38. while(scanf("%d", &n), n != 0)
  39. {
  40. int i;
  41. string str;
  42. map<string, int> ma;
  43. for(i = 1; i <= n; i++)
  44. {
  45. cin>>str;
  46. ma[str] = i;
  47. }
  48. scanf("%d", &m);
  49. double rate;
  50. string str2;
  51. while(m--)
  52. {
  53. cin>>str>>rate>>str2;
  54. g[ma[str]][ma[str2]] = rate;
  55. }
  56. if(bellman() == 0)
  57. printf("Case %d: Yes\n", t);
  58. else
  59. printf("Case %d: No\n", t);
  60. ma.clear();
  61. t++;
  62. }
  63. return 0;
  64. }

poj 2240 Arbitrage bellman-ford算法的更多相关文章

  1. POJ 2240 Arbitrage(Floyed-Warshall算法)

    题意:给出n种货币,m种兑换比率(一种货币兑换为另一种货币的比率),判断测试用例中套汇是否可行.(套汇的意思就是在经过一系列的货币兑换之后,是否可以获利.例如:货币i→货币j→货币i,这样兑换后,是否 ...

  2. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  3. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

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

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

  5. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  6. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  7. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  8. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  9. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

随机推荐

  1. jQuery Mobile_表单元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. python 字典访问的三种方法

    定义字典 dic = {'a':"hello",'b':"how",'c':"you"} 方法一: for key in dic: prin ...

  3. java file的一些方法

    file包下的一些方法:       File file = new File("d:\\", "tea.txt");         //文件名        ...

  4. 数据接口管理工具 thx RAP

    RAP是数据接口管理工具.在开发时前端将请求转至RAP,由RAP提供模拟数据:而后端使用RAP测试接口的正确性.这样RAP就成为了开发过程中的强 依赖,进而确保接口文档的实时正确性.RAP采用JSON ...

  5. 文件/目录部分处理工具类 DealDir.java

    package com.util; import java.io.File; import java.util.StringTokenizer; /** * 文件/目录 部分处理 * @createT ...

  6. Linux 环境下开机自启动Oracle服务

    使用如下命令查看Oracle相关服务是否已启动: ps aux | grep ora_ #若无ora_**_**相关的进程,则oracle数据库实例未启动 netstat -tlnup | grep ...

  7. [原]网络库libevent在Visual Studio中的使用方法

    libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制.著名分布式缓存软件memcached也 ...

  8. objective-c 随便记记

    1.tableview滚动到某一位置 [tableViewShow setContentOffset:CGPointMake(0, 0) animated:YES]; //解决tableView分割线 ...

  9. 理解Javascript参数中的arguments对象

    ECMAScript中函数没有标签名的特性,所以ECMAScript函数中没有重载. Javascript中arguments的存在可以弥补javascript中函数没有重载的不足. Javascri ...

  10. vue通过判断写样式(v-bind)

    v-bind:style="$index % 2 > 0?'background-color:#FFF;':'background-color:#D4EAFA;'"