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. 【C++】《Effective C++》第七章

    第七章 模板与泛型编程 条款41:了解隐式接口和编译期多态 面向对象设计中的类(class)考虑的是显式接口(explict interface)和运行时多态,而模板编程中的模板(template)考 ...

  2. 【Linux】CentOS7中yumbackend.py进程的结束方法

    环境: CentOS Linux release 7.3.1611 (Core) 今天启动这个不怎么用的机器,才启动,就发现后台的yum无法进行安装,持续报这个错误 Loaded plugins: f ...

  3. python—打开图像文件报错

    今天使用python打开一张图像文件的时候报错了 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illeg ...

  4. 【译】Async/Await(三)——Aysnc/Await模式

    原文标题:Async/Await 原文链接:https://os.phil-opp.com/async-await/#multitasking 公众号: Rust 碎碎念 翻译 by: Praying ...

  5. css全站变灰

    2020年4月4日全国哀悼日这一天,我发现不少网址都变灰了,我第一想法就是怎么做到的?不可能换素材整个网址重做一遍吧?后面发现是用的其实是css的filter滤镜: grayscale可以将图像转化为 ...

  6. 简单明朗的 RNN 写诗教程

    目录 简单明朗的 RNN 写诗教程 数据集介绍 代码思路 输入 and 输出 训练集构建 生成一首完整的诗 代码实现 读取文件 统计字数 构建word 与 id的映射 转成one-hot代码 随机打乱 ...

  7. 阿里云 CentOS7中搭建FTP服务器

    1配置 vsftpd-3.0.2-27.el7.x86_64 阿里云 centos 7.0 2 ftp工作模式 2.1 ftp通道 ftp工作会启动两个通道: 控制通道,数据通道 在ftp协议中,控制 ...

  8.  打开APP  04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋

     打开APP  04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋

  9. 思考gRPC :为什么是HTTP/2

    Introducing gRPC Support with NGINX 1.13.10 - NGINX https://www.nginx.com/blog/nginx-1-13-10-grpc/ 思 ...

  10. (008)每日SQL学习:Oracle Not Exists 及 Not In 使用

    今天遇到一个问题,not in 查询失效,我以为是穿越了,仔细查了点资料,原来理解有误! select value from temp_a a where a.id between 1 and 100 ...