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;
}


相关文章
|
存储 人工智能
高精度大数(超long long)取余原理及模板
高精度大数(超long long)取余原理及模板
462 0
|
2月前
|
安全 网络协议 网络安全
2025年第十六届蓝桥杯网络安全CTF国赛总决赛真题详解Writeup(Web漏洞挖掘、Crypto密码学、Misc杂项、Reverse逆向、Pwn二进制漏洞)
2025年第十六届蓝桥杯网络安全CTF国赛总决赛真题详解Writeup(Web漏洞挖掘、Crypto密码学、Misc杂项、Reverse逆向、Pwn二进制漏洞)
2025年第十六届蓝桥杯网络安全CTF国赛总决赛真题详解Writeup(Web漏洞挖掘、Crypto密码学、Misc杂项、Reverse逆向、Pwn二进制漏洞)
|
9月前
|
缓存 网络协议 安全
即时通讯初学者必知必会的20个网络编程和通信安全知识点
即时通讯IM应用开发的初学者很容易迷失在网络编程的复杂性以及通信安全的各种概念里,本文不涉及深度理论知识,尽量通过一句话或几句话让你快速了解20个相关的网络编程和通信安全知识点,希望能助你愉快地开始即时通讯应用开发。
357 0
|
程序员 API 数据安全/隐私保护
Flink--8、时间语义、水位线(事件和窗口、水位线和窗口的工作原理、生产水位线、水位线的传递、迟到数据的处理)
Flink--8、时间语义、水位线(事件和窗口、水位线和窗口的工作原理、生产水位线、水位线的传递、迟到数据的处理)
|
12月前
|
安全 算法 网络安全
网络安全的盾牌与剑:漏洞防御与加密技术深度解析
在数字信息的海洋中,网络安全是航行者不可或缺的指南针。本文将深入探讨网络安全的两大支柱——漏洞防御和加密技术,揭示它们如何共同构筑起信息时代的安全屏障。从最新的网络攻击手段到防御策略,再到加密技术的奥秘,我们将一起揭开网络安全的神秘面纱,理解其背后的科学原理,并掌握保护个人和企业数据的关键技能。
343 3
|
Java 程序员
Java中实现动态性的原理和机制
Java中实现动态性的原理和机制
280 1
|
NoSQL Java 数据库连接
MongoDB Java
10月更文挑战第18天
204 3
|
SQL 关系型数据库 MySQL
mysql密码错误-ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using passwor:yes)
这篇文章提供了解决MySQL数据库"Access denied for user 'root'@'localhost' (using password: YES)"错误的方法,通过跳过密码验证、修改root密码,然后重启服务来解决登录问题。
mysql密码错误-ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using passwor:yes)
第十九届浙大城市学院程序设计竞赛(F、L)
第十九届浙大城市学院程序设计竞赛(F、L)
150 0
|
安全 Java 开发者
Java并发编程中的线程安全策略
在现代软件开发中,Java语言的并发编程特性使得多线程应用成为可能。然而,随着线程数量的增加,如何确保数据的一致性和系统的稳定性成为开发者面临的挑战。本文将探讨Java并发编程中实现线程安全的几种策略,包括同步机制、volatile关键字的使用、以及java.util.concurrent包提供的工具类,旨在为Java开发者提供一系列实用的方法来应对并发问题。
148 0