题目描述

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

题解:不同于我之前写的求正环 I ,这道题随机的选取一个货币种类,然后进行操作,又回到当前种类的时候,判断金币是否增加
因为n比较小,这里我们可以用floyed的算法,如果大于1 则说明金币增加了

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;
double arr[50][50];
map<string , int> mp;
int n, m, num;
bool flo()
{for(int k = 1; k <= n; k++)for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)arr[i][j] = max(arr[i][j], arr[i][k] * arr[k][j]);  //表示i 兑换乘 j 时的最大货币数for(int i = 1; i <= n; i++)if(arr[i][i] >1) return true;  //如果大于1 则说明货币增加了return false;
}
int main()
{string s1, s2;double b;int q = 1;while(cin >> n && n){num = 1;mp.clear();for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)arr[i][j] = i == j ? 1 : 0;  //i 到 i的兑换比率为1for(int i = 0; i < n; i++){cin >> s1;if(!mp[s1]){mp[s1] = num++;  //将名字转化为编号}}cin >> m;int k = 0;for(int i = 0; i < m; i++){cin >> s1 >> b >> s2;arr[mp[s1]][mp[s2]] = b;  // s1 到 s2 的兑换比率为b}printf("Case %d: ", q++);if(flo()){cout << "Yes" <<endl;}else cout << "No" << endl;}return 0;
}

I - Arbitrage(判断是否有无正环 II)相关推荐

  1. Currency Exchange(判断有无正环)

    题目描述 Several currency exchange points are working in our city. Let us suppose that each point specia ...

  2. Arbitrage(判断正环 spfa写法)

    题目如下: Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a cu ...

  3. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. F - Wormholes(判断是否存在负环)

    ##题目描述 While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A w ...

  5. 牛客多校2 - Link with Game Glitch(spfa求正环,套汇,二分答案)

    https://ac.nowcoder.com/acm/contest/33187/D 题意 给定 n 种物品,一共 m 种转换方案. 每种转换方案给出四个数 a , b , c , d a, b, ...

  6. POJ 2240 Arbitrage(SPFA判正环)

    POJ 2240 Arbitrage 题目大意 套利是指利用货币汇率的差异,将一种货币的一个单位转换为同一货币的多个单位.例如,假设1美元买0.5英镑,1英镑买10.0法国法郎,1法国法郎买0.21美 ...

  7. 判断图有无环_链表:环找到了,那入口呢?

    关于代码的一切尽在「代码随想录」 ❝ 找到有没有环已经很不容易了,还要让我找到环的入口? ❞ 第142题.环形链表II 题意:给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 nul ...

  8. 货币兑换(判断正环)

    货币兑换 POJ - 1860 我们城市有几个货币兑换点.让我们假设每个点专门研究两种特定的货币,并且只与这些货币进行兑换操作.可以有多个点专门用于同一对货币.每个点都有自己的汇率,A到B的汇率就是1 ...

  9. 判断图有无环_判断无向图/有向图中是否存在环

    本文主要针对如何判断有向图/无向图中是否存在环的问题进行简单的论述. 一 无向图 1.利用DFS进行判断 利用DFS判断有向图是否存在环,是最为常用的一种方法,虽然这种方法很常用,但可参考的代码的实现 ...

最新文章

  1. Python字符串方法用示例解释
  2. C#列出局域网中可用SQL Server服务器(续)
  3. vuex分模块后,如何获取state的值
  4. python下载matplotlib.finance模块_关于Matplotlib中No module named 'matplotlib.finance'的解决办法...
  5. 插入模板_WordPress在文章列表和内容页插入广告
  6. Java构造函数可以私有,我们可以在Java中使用私有的构造函数吗?
  7. 初探SQL Server 2008商业智能
  8. Java基础题笔记1
  9. 解读webpack的bundle.js
  10. 《编译原理》学习笔记 ·003【第二章:文法和语言(形式语言理论)-2】
  11. 网络流24题(更新中
  12. C#.net拖拽实现获得文件路径
  13. 详细讲述matlab中矩阵的卷积函数convn
  14. 计算机矩阵入门(eigen)0XC000041D
  15. html复制粘贴的文字自动换行,word中复制的文字出现自动换行怎么办
  16. 计算机如果没有什么 就无法启动,电脑开机没有任何反应
  17. Unity简单使用Job System
  18. JavaScript设计模式综合应用案例
  19. Word中论文参考文献英文字符间距太大,调整方法。
  20. cocos2dx-js 的配置和安装

热门文章

  1. Java Persistence with MyBatis 3(中国版)
  2. 【c语言】用指针变量输出一维数组中的数据
  3. WPF TreeViewItem
  4. hibernate、mysql、中文字符问题
  5. WCF元数据交互及其序列化
  6. 设计模式--适配器(Adapter)
  7. cn.cw.gps.domain.VisitReport.setVisitID([Ljava.lang.String;)]
  8. python中的断言
  9. webstorm的个性化设置settings
  10. opengl笔记—— glMultMatrixf() 区别 glLoadMatrixf()