Floyd-Warshall算法的应用——杭电OJ 1217 Arbitrage
来源:互联网 发布:宁波大数据管理局 编辑:程序博客网 时间:2024/06/05 08:25
Arbitrage
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.
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.
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
3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0
Sample Output
Case 1: YesCase 2: No
#include <stdio.h>#include <string.h>int n, m;char currency[40][50];double map[40][40];/*num函数给输入的货币编号*/int num(char c[]);int main(){//freopen("data.in", "r", stdin);int i, k, j, kase = 0, flag;double rate;char c1[50], c2[50];while (scanf("%d", &n) != EOF && n) {for (i = 1; i <= n; i++)scanf("%s", currency[i]);/*初始化二维数组map*/ memset(map, 0, sizeof(map));for (i = 1; i <=n; i++)map[i][i] = 1;/*对输入的货币编号,并存入map中*/scanf("%d", &m);while (m--) {scanf("%s %lf %s", c1, &rate, c2);map[num(c1)][num(c2)] = rate;}/*Floyd-Warshall算法核心部分*/ for (k = 1; k <= n; k++)for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)if (map[i][j] < map[i][k] * map[k][j]) /*根据题意,有所改动*/ map[i][j] = map[i][k] * map[k][j];/*判断部分*/ flag = 0;for (i = 1; i <= n; i++)if (map[i][i] > 1) {flag =1;break;}if (flag)printf("Case %d: Yes\n", ++kase);elseprintf("Case %d: No\n", ++kase);}return 0;}int num(char c[]) {int i;for (i = 1; i <= n; i++)if (strcmp(c, currency[i]) == 0)return i;}
0 0
- Floyd-Warshall算法的应用——杭电OJ 1217 Arbitrage
- Floyd—Warshall算法
- Floyd-Warshall算法及其应用
- 只有五行的算法——Floyd-Warshall
- 6.1只有五行的算法——Floyd-Warshall
- 只有五行的算法——多源最短路径 Floyd-Warshall
- 多源最短路径——floyd-Warshall算法的拓展
- poj-2240 Arbitrage(最佳路径 Floyd-Warshall算法)
- 杭电 ACM HDU 1217 Arbitrage(最短路 floyd算法)
- Floyd-Warshall算法的原型
- floyd—warshall
- 【最短路+floyd】杭电 hdu 1217 Arbitrage
- hdu 1217 Arbitrage(Floyd最短路算法+map应用)
- 算法——所有节点对的最短路径:Floyd-Warshall算法、Johnson算法
- 【算法】全局最短路——Floyd-Warshall算法
- poj 1125 Stockbroker Grapevine——Floyd-Warshall算法
- 最短路径之Floyd—Warshall算法
- 最短路径Ⅲ—Floyd-Warshall算法
- MySQL
- caffe 版本 yolo 过程记录
- 设计包含多种控件的用户登录界面(1)
- 饿了么移动APP的架构演进
- 很多开发都会用到的MVC、MVP、MVVM 架构
- Floyd-Warshall算法的应用——杭电OJ 1217 Arbitrage
- KMP之暴力匹配
- Android-Fragment生命周期
- Xshell4配色方案
- 天鹅、大虾和梭鱼--ruby的动态派发
- OJ---字符串通配符
- 初学android-自定义控件以及控件属性
- qt 文件操作
- 100岁老龙虾没有被吃掉,却死在营救途中