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
这题可以用floyd和map做,map用来存货币的种类,再用floyd判断经过其他货币中转到自己的时候有没有汇率大于1的情况。
#include<stdio.h>
#include<map>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
char s[100][100],s1[100],s2[100];
double dis[100][100];
int flag,n,index;
double max(double a,double b)
{
return a>b?a:b;
}

void floyd()
{
int i,j,k;
for(k=1;k<=index;k++){
for(i=1;i<=index;i++){
for(j=1;j<=index;j++){
dis[i][j]=max(dis[i][j],dis[i][k]*dis[k][j]);
}
}
}
for(i=1;i<=index;i++){
if(dis[i][i]>1){
flag=1;break;
}
}
return;
}

int main()
{
int m,i,j,num=0;
double a;
while(scanf("%d",&n)!=EOF && n!=0)
{
map<string,int> hash;
hash.clear();
memset(dis,0,sizeof(dis));
index=0;
for(i=1;i<=n;i++){
scanf("%s",s[i]);
if(hash[s[i]]==0){
index++;
hash[s[i]]=index;
}
}
flag=0;
scanf("%d",&m);
for(i=1;i<=m;i++){
scanf("%s%lf%s",s1,&a,s2);
if(strcmp(s1,s2)==0 && a>1)
flag=1;
if(dis[hash[s1]][hash[s2]]<a){
dis[hash[s1]][hash[s2]]=a;
}
}
printf("Case %d: ",++num);
//printf("%d\n\n",index);
if(flag==1){
printf("Yes\n");continue;
}

floyd();
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}

hdu1217 Arbitrage的更多相关文章

  1. HDU1217:Arbitrage(SPFA)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意 在每种钱币间进行各种交换,最后换回自己如果能赚,那么就Yes,否则No 注意应为有负权 ...

  2. DFS判断正环

    hdu1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. 【floyed】【HDU1217】【Arbitrage】

    题目大意: 给你几种货币,以及几种汇率关系,问是否存在套利的可能? 思路: 初步想法:图上存在一个环的路径上权值相乘大于1.... 再者:该如何找到图上所有环呢.... 好吧 经过鸟神 和 况神的指点 ...

  4. Arbitrage HDU1217

    汇率转换问题: 怎么样才能套利 可以用Floyd算法: #include<bits/stdc++.h> using namespace std; ][]; int main() { int ...

  5. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  6. poj 2240 Arbitrage

    Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java class name ...

  7. UVa 104 - Arbitrage(Floyd动态规划)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  8. Arbitrage(bellman_ford)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16652   Accepted: 7004 Descri ...

  9. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

随机推荐

  1. Python列表推导式玩法

    前言 列表做为python的基础,是必须学习的语法之一.一些基础的之前已经是反复温习和使用了,今天我们来学习它的进阶版-->列表推导式. 列表推导式: 优点:是将所有的值一次性加载到内存中,相比 ...

  2. python函数2-函数参数

    rgb法则:

  3. 【Oracle】删除表空间

    删除表空间如果是 SQL> DROP TABLEPSACE XXXX; 是无法将数据文件一同都删除的 想要删除表空间和数据文件需要如下操作: SQL> drop tablespace XX ...

  4. JDBC入门程序总结

    JDBC本质 只是一个接口 每个数据库的规范 就是实现类的接口 其实是官方 定义的一套操作所有关系型数据库的规则,就是接口,各个数据库厂商去实现这套接口,提供数据库驱动jar包, 我们可以使用这套接口 ...

  5. MySQL索引性能分析

    为什么要做性能分析 你有没有这样的情况. 面对一个你没怎么写过的.复杂的业务,你构思了很久,终于开始敲下了第一段代码. 写的过程迷迷糊糊,有的时候还能把自己搞晕了. 但你还是终于把它写完了. 但是点击 ...

  6. 2021年首届.NET线下沙龙上海站 - 2021 .NET Meetup in Shanghai

    .NET Conf 2020 刚刚在苏州落下帷幕, .NET 开发者们的热情不减,来自五湖四海的朋友一起参加疫情之下的 .NET 盛会. 2021年上海第一场线下活动就要来了,快来加入我们一起学习.N ...

  7. Markdown特殊字符、数学公式汇总

    引自:https://blog.csdn.net/weixin_39653948/article/details/104621249

  8. 配置 containerd 镜像仓库完全攻略

    作者简介 王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营.拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Lin ...

  9. 从零开始学spring源码之ioc预热:bean的拓展和beanProcessor注册

    上篇聊完了bean的解析,说起来做的事情很简单,把xml文件里面配置的标签全部解析到spring容器里面,但是spring做的时候,花了那么大代价去做,后面看看到底值不值得呢. 接下来看看prepar ...

  10. Hmailserver搭建邮箱服务器

    由于阿里云,谷歌云,腾讯云等服务器都不开放25端口和pop3端口,想要使用邮箱服务得购买他们的企业邮箱,但是对于个人而言比较贵. 所以我们需要利用家庭宽带申请公网IP. 首先打电话给运营商客服,申请动 ...