Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10439    Accepted Submission(s): 4721

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美元,问通过不同钱币之间转换最后能不能赚钱。

题解:因为输入的钱币种类是字符串,要用map转换一下,变成方便计算的数字编号。存起来用floyd算法跑一遍,最后看a[1][1]是不是比1大,如果最后比1大,就说明赚钱了。注意:初始化数组将a[1]a[1]设为1,其他设为0.因为是double数组,所以不要用memset去初始化。

 #include<bits/stdc++.h>
using namespace std;
int n,m;
double a[][];
map<string,int>s;
void init()
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
a[i][j]=;
}
}
a[][]=1.0;
}
bool floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
a[i][j]=max(a[i][j],a[i][k]*a[k][j]*1.0); }
}
}
if(a[][]>1.0)return true;
return false;
}
int cases=;
int main() {
while(~scanf("%d",&n),n)
{
init();
cases++;
//int num=0;
for(int i=;i<=n;i++)//字符串转换
{
char temp[];
scanf("%s",temp);
s[temp]=i;
}
scanf("%d",&m);
for(int i=;i<m;i++)
{
char temp1[],temp2[];
double temp3;
scanf("%s %lf %s",temp1,&temp3,temp2);//存两种钱币的转换比率
if(temp3>a[s[temp1]][s[temp2]])
a[s[temp1]][s[temp2]]=temp3;
}
if(floyd())printf("Case %d: Yes\n",cases);
else printf("Case %d: No\n",cases);
//printf("\n");
}
return ;
}

hdu1217Arbitrage(floyd+map)的更多相关文章

  1. POJ 2263 Heavy Cargo(Floyd + map)

    Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3768   Accepted: 2013 Descr ...

  2. hdu1546Idiomatic Phrases Game(floyd+map)

    传送门 成语接龙,找每个单词都需要一点时间,问最少的时间 把字符串用map处理成数字编号,之后用floyd #include<bits/stdc++.h> using namespace ...

  3. hdu2112HDU Today(floyd+map数组对字符串的应用)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑

    倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...

  5. 【BZOJ 1491】 [NOI2007]社交网络

    Description Input Output 输出文件包括n 行,每行一个实数,精确到小数点后3 位.第i 行的实数表 示结点i 在社交网络中的重要程度. Sample Input 4 4 1 2 ...

  6. 洛谷P2402 奶牛隐藏

    洛谷P2402 奶牛隐藏 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛混乱的原因看题目描述) 题目描述 在一个农场里有n块田地. ...

  7. hdu 2923 map+Floyd 拉破车

    有向图 具体方向看箭头 从起点到指定城市拉破车,一个城市可能有多个破车,一次只能拉一辆破车 也就是到了指定地点后要回到起点 假如有100辆破车 但是只有一个城市有 就得在起点与这个城市间往返100次所 ...

  8. POJ 2240 【这题貌似可以直接FLOYD 屌丝用SPFA通过枚举找正权值环 顺便学了下map】

    题意: 给了n种硬币的名称,给了m种硬币间的转换关系. 从任意兑换地点开始兑换,看是否能够通过兑换的方式增加金钱. 思路: 用SPFA不断对各个点进行松弛操作,寻找正权值的环.如果找到则输出Yes. ...

  9. floyd算法学习笔记

    算法思路 路径矩阵 通过一个图的权值矩阵求出它的每两点间的最短路径矩阵.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1):又 ...

随机推荐

  1. Linux学习总结(六)-su命令 sudo 命令 限制root远程登录

    root 用户拥有至高无上的权利,那么我们运维人员是不是直接在root用户下处理所有问题呢? 答案是否定的,权力越大,责任越大,人是会犯错的,因此我们要在不影响我们的工作情况下,尽量限制我们的权力,以 ...

  2. Sublime 快捷键一览表

    Sublime Text 快捷键一览表 Sublime Text比起Notepad++,UltraEdit之类Code编辑器来说,其功能有过之而无不及,配合着各种插件使用,Sublime Text在实 ...

  3. PAT——1001 害死人不偿命的(3n+1)猜想 (15)

    对给定的任一不超过1000的正整数n,简单地数一下,需要多少步(砍几下)才能得到n=1? 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值. 输出格式:输出从n计算到1需要的步数. 输入样例 ...

  4. 跟我一起写 Makefile (Linux )

    1.昨天 在 Linux 下用  touch  指令 新建了一个  hello.c 并且使用 vim 编辑器 写了代码 ,使用 gcc 指令编译 最后运行 成功了 .具体方式如下: 在Linux 根目 ...

  5. [译文][转载]greenlet:轻量级并发程序

    英文原文地址:https://greenlet.readthedocs.io/en/latest/中文翻译转载地址:https://zhuanlan.zhihu.com/p/25188731 背景 g ...

  6. linux 安装 node 环境

    本篇学习的分享主要说在linux 安装 node 环境,个人也是在腾讯云的实验室课程学习的,这里只是个人的一个学习记录, 大家也可以去腾讯的实验室来体验一下,教程十分详细易学. 1 .安装 Node. ...

  7. ES6 开发规范-最佳实践

    ES6 开发规范(最佳实践) 本文为开发规范,收集方便日后查看. [开发规范]https://blog.csdn.net/zzzkk2009/article/details/53171058?utm_ ...

  8. jquery实现表单验证简单实例

    /* 描述:基于jquery的表单验证插件. */ (function ($) { $.fn.checkForm = function (options) { var root = this; //将 ...

  9. linux 下安装 Cisco Packet Tracer 7.11以及一些注意

    https://blog.csdn.net/qq_35882901/article/details/77652571 https://linux.cn/article-5576-1.html 开启登录 ...

  10. C# base64 转 byte[]

    string转成 Base64 形式的String //byte[] 转string byte[] b = Encoding.Default.GetBytes("字符串"); // ...