Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30790   Accepted: 12761

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
  3.  

用spfa解决POJ2240,因学习spfa 的时间较短,犯了不少错误,在代码行中,会注释出来以备察看一些新手可能需要注意的地方,接下来看题意....

题意:给你一些钱币,看你是否能通过汇率转换,赚到更多的钱。

例如给出   :

  我们看第一个样例,1美元可以兑换0.5英镑,1英镑可以兑换10法郎,但1法郎只能兑换0.21美元。

  那么如果你有1美元,我们先兑换成英镑,在兑换成法郎,最后兑换成美元  == 1*0.5*10*0.21=1.05美元,,,明显看出,经过一系列的兑换之后,我们的金钱更多了....那么再看第二个样例, 我们显然可以知道,此题的题意就是让我们求出一条兑换钱币的次序,或者说路径,看是否能有一条路径兑换之后,我们的钱币比原来更多了。

  那么接下来看代码吧..

  1.  
  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <string.h>
  4. #include <queue>
  5. #define MAXN 31
  6. int n,m,head[MAXN],vis[MAXN],cnt[MAXN];
  7. char name[][];  //用来读入各种货币的名称
  8. using namespace std;
  9. typedef struct
  10. {
  11. int start,end;
  12. int next;
  13. double w;
  14. } Edge;
  15. Edge edges[];
  16. double dis[MAXN];
  17.  
  18. int Find(char str1[])
  19. {
  20. for(int i=; i<=n; i++)
  21. if(strcmp(name[i],str1)==)
  22. return i;//没有考虑找不到的情况,但是实际上,题目不会给出找不到的货币名称....
  23. return -;
  24. }
  25.  
  26. bool spaf(int s)
  27. {
  28. queue<int>que;
  29. memset(dis,,sizeof(dis));
  30. memset(vis,false,sizeof(vis));
  31. memset(cnt,,sizeof(cnt));
  32. vis[s]=dis[s]=;    //这里一开始学习spfa的时候,还保留着用其他方法的习惯,总给vis[1]置为1 ,,但其实这里应该写成vis[s]=1,不然题目就会出现一些小错误,导致过了样例,却还是wa了
  33. que.push(s);
  34. while(!que.empty())
  35. {
  36. int temp=que.front();
  37. que.pop();
  38. vis[temp]=;
  39. for(int i=head[temp]; i!=-; i=edges[i].next)
  40. {int x=edges[i].start,y=edges[i].end;
  41. double w=edges[i].w;
  42. if(dis[y]<dis[x]*w)
  43. {
  44. dis[y]=dis[x]*w;
  45. if(!vis[y])
  46. {
  47. vis[y]=;
  48. if(++cnt[y]>n)    //为了防止某一个节点无限被调用,形成环路,则可以一直赚钱,卡在这里
  49. return true;
  50. que.push(y);
  51. }
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57. int main()
  58. {
  59. int len=;
  60. char start[],end[];
  61. while(scanf("%d",&n)&&n!=)
  62. {
  63. getchar();
  64. for(int i=; i<=n; ++i)
  65. gets(name[i]);
  66. scanf("%d",&m);
  67. memset(head,-,sizeof(head));
  68. for(int i=; i<m; ++i)
  69. {
  70. scanf(" %s %lf %s",start,&edges[i].w,end);
  71. edges[i].start=Find(start);
  72. edges[i].end=Find(end);
  73. edges[i].next=head[edges[i].start];
  74. head[edges[i].start]=i;
  75. }
  76. m=;
  77. for(int i=; i<=n; ++i)
  78. {
  79. if(spaf(i)==true)    
  80. {
  81. m=;
  82. break;
  83. }
  84. }
  85. if(m)
  86. printf("Case %d: Yes\n",len++);
  87. else
  88. printf("Case %d: No\n",len++);
  89. }
  90. return ;
  91. }

用SPFA 解决POJ2240的更多相关文章

  1. SPFA解决单源最短路径

    SPFA(Shortest Path Faster Algorithm): 一:基本算法 在求解单源最短路径的时候,最经典的是 Dijkstra 算法,但是这个算法对于含有负权的图就无能为力了,而 B ...

  2. lightoj 1074 spfa判断负环

     Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Sub ...

  3. BZOJ 1003 物流运输 题解 【SPFA+DP】

    BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...

  4. [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA

    考试的时候忘了缩点,人为dfs模拟缩点,没想到竟然跑了30分,RB爆发... 边是可以重复走的,所以在同一个强连通分量里,无论从那个点进入从哪个点出,所有的点一定能被一条路走到. 要使用缩点. 然后我 ...

  5. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  6. UESTC - 1987 童心未泯的帆宝和乐爷 (第k短路 A*算法+SPFA算法 模板)

    传送门: http://www.qscoj.cn/#/problem/show/1987 童心未泯的帆宝和乐爷 Edit Time Limit: 10000 MS     Memory Limit: ...

  7. 【BZOJ 2595】2595: [Wc2008]游览计划 (状压DP+spfa,斯坦纳树?)

    2595: [Wc2008]游览计划 Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 1572  Solved: 7 ...

  8. 【题解】洛谷P2296 [NOIP2014TG] 寻找道路(SPFA+DFS)

    题目来源:洛谷P2296 思路 一开始看还以为是一道水题 虽然本来就挺水的 本道题的难点在于如何判断是否路径上的点都会直接或者间接连着终点 我们需要在一开始多建一个反向图 然后从终点DFS回去 把路径 ...

  9. 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson

    根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...

随机推荐

  1. P1359 租用游艇 && P3905 道路重建 ------Floyd算法

    P1359 租用游艇   原题链接https://www.luogu.org/problemnew/show/P1359 P3905 道路重建   原题链接https://www.luogu.org/ ...

  2. Codeforces 1111 E. Tree(虚树,DP)

    题意 有一棵树,q个询问,每次询问,指定一个点做树根,再给定k个点,要求把这些点分成不超过m组的方案数,分配的限制是任意两个有祖先关系的点不能分在同一组.题目还保证了所有的询问的k加起来不超过1e5. ...

  3. redis基础操作概念等笔记

    Redis常用配置 daemonize ->是否是后台进程 port ->对外端口 logfile ->Redis 系统日志 dir ->Redis 工作目录 Redis的链接 ...

  4. centos7下面安装tomcat

    前言 对于一个新安装的 centos 系统来说,是没有 tomcat 服务器的.用下面的命令可以查看 tomcat 服务的状态. systemctl status tomcat.service//或者 ...

  5. 20.包含min函数的栈 Java

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 思路 借助辅助栈实现: 压栈时:若辅助栈为空,则将节点压入辅助栈.否则,当当前节点小于 ...

  6. golang——reverse反转字符串

    reverse反转,是个比较基础算法.要实现这个方法,从常理考虑可以申请一个新空间,然后将字符串的从尾到头依次填充该空间,最后新空间的内容就是反转后的结果了,这个方式的算法复杂度是O(n),并且还需要 ...

  7. 使用create-react-app创建项目(二)——引入ant方法(一)

    扩展项目(需要创建git默认文件) 具体步骤如下:       a.git init       b.git add .       c.git commit -m "..." n ...

  8. js 时间戳格式化日期格式

    时间戳转换为日期,网上搜了好几个或多或少都有点问题,自己整理了一下,写了个方法 console.log(formatDate(1565280000000)) 输出: 2019-08-09 00:00: ...

  9. 14 statefulset (sts)控制器

    statefulset (sts)控制器 可以用于部署有状态的服务,比如说redis,mysql ,zk等等... 1. 稳定且唯一的网络标志符:2. 稳定且持久的存储3. 有序,平滑地部署和扩展:4 ...

  10. [Jenkins]局域网内可访问配置方法 -windows

    如果是把jenkins.war放在tomcat中运行,则当jenkins宿主机,启动tomcat服务之后,则直接可以通过局域网访问jenkins  下面这种情况是,直接通过jenkins.exe安装的 ...