HDU-1217,Arbitrage(Floyd加法变乘法)

简介: HDU-1217,Arbitrage(Floyd加法变乘法)

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


解题思路:


因为题中货币汇率的转换使用的是乘法,而与小于1的数相乘是会导致原来的数变小,所以此题相当于是有负权边的题目,那么Dijkstra算法是不能使用的。题目问套利是否存在,因此每种货币是否存在套利都应该考虑,Floyd算法的耗时不会太长。然而题目中给出的任意两种货币不一定能够互相转换,那么不能够转换的话我们用0表示,使用Floyd算法求每两种货币能够互相转换的最大汇率,那么最后我们判断每种货币转换为自己时的汇率是否大于1,是的话说明存在套利,否的话则说明本货币不存在套利。注意本题是单向图。。。


程序代码:  


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max(a,b)(a>b?a:b)
#define N 50
char str[N][N];//存储货币名称 
double map[N][N];//存储汇率 
int n,m;
int count(char s[])//计算字符串编号 
{
  int i;
  for(i=0;i<n;i++)
  {
    if(strcmp(s,str[i])==0)//匹配到相符的字符串 
      return i;
  }
  return -1;
}
int Floyd()
{
  int i,j,k;
  for(k=0;k<n;k++)
  {
    for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
      {
        map[i][j]=max(map[i][j],map[i][k]*map[k][j]);
      }
    }
  }
  for(i=0;i<n;i++)//判断是否存在套利 
  {
    if(map[i][i]>1)//货币转换成自身大于1则存在套利 
      return 1;
  }
  return 0;
}
int main()
{
  int i,ans=1;
  char s1[N],s2[N];
  double x;
  while(~scanf("%d",&n))
  {
    if(n==0)
      break;
    memset(map,0,sizeof(map));
    for(i=0;i<n;i++)
      scanf("%s",str[i]);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
      scanf("%s%lf%s",s1,&x,s2);
      map[count(s1)][count(s2)]=x;
    }
    printf("Case %d: ",ans++);
    if(Floyd()==1)
      printf("Yes\n");
    else
      printf("No\n");
  }
  return 0;
}


相关文章
|
4月前
|
机器学习/深度学习 存储 人工智能
每日练习之矩阵乘法——斐波那契公约数
每日练习之矩阵乘法——斐波那契公约数
35 0
|
5月前
|
人工智能 算法
DAY-1 | 迭乘法、辗转相除法、试除法:最大公约数与最小公倍数问题
这段内容是一个关于计算两个数的最大公约数(Greatest Common Divisor, GCD)和最小公倍数(Least Common Multiple, LCM)的编程题目说明,包括题干、题解和方法总结。其中提到了两种方法:辗转相除法和试除法。辗转相除法通过不断用较大数除以较小数直到余数为零来求最大公约数,然后利用两数乘积除以最大公约数得到最小公倍数。试除法则是通过循环尝试两数的倍数是否同时能被两数整除来求解。在方法总结部分,还介绍了迭乘法求最小公倍数的方法。
59 0
|
机器学习/深度学习 算法
【Leetcode】面试题 16.05. 阶乘尾数、HJ7 取近似值
目录 面试题 16.05. 阶乘尾数 HJ7 取近似值
66 0
|
5月前
|
C++
【PTA】​ L1-080 乘法口诀数列​(C++)
【PTA】​ L1-080 乘法口诀数列​(C++)
88 0
【PTA】​ L1-080 乘法口诀数列​(C++)
|
5月前
|
存储 C++
[C++/PTA] 矩阵的乘法运算
[C++/PTA] 矩阵的乘法运算
134 0
|
算法 C++
剑指offer(C++)-JZ65:不用加减乘除做加法(算法-位运算)
剑指offer(C++)-JZ65:不用加减乘除做加法(算法-位运算)
数学问题之(矩阵加速递推快速幂)
数学问题之(矩阵加速递推快速幂)
|
测试技术
【牛客】WY49数对,JZ65不用加减乘除做加法
【牛客】WY49数对,JZ65不用加减乘除做加法
153 0
【牛客】WY49数对,JZ65不用加减乘除做加法
|
人工智能
唯一分解定理(算术基本定理)详解——hdu5248和lightoj1341
任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积 N=P1a1 P2a2P3a3…Pnan,这里P1<P2<P3…<Pn均为质数,其中指数ai是正整数。这样的分解称为 N 的标准分解式.
294 0