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. wampserver 最新版本 mysql修改数据库密码

    由于wampsever版本更新就导致以前版本的密码修改造成失败,主要是密码字段改变造成的! 第一步 进入MySQL 控制台  wamp安装,数据库是没有密码 进入控制台直接回车就可以了 第二步 使用 ...

  2. table中实现数据上移下移效果

    html 由于vue+Element项目中的table,没有开放的上移下移的api,但是能对数据操作,故思路为数组中的一条数据,再重新添加一条数据,办法有点笨,但是好歹也是实现了,望有好的办法的,请留 ...

  3. Leetcode225 用栈实现队列

    大众思路: 用两个栈实现,记为s1,s2 1.元素入栈时,加入s1 2.元素出栈时,对s2进行判断,如果s2为空,则将全部s1元素弹出并压入到s2,然后从s2栈顶弹出一个元素:如果s2不为空,则直接从 ...

  4. if __name__ == "__main__"如何正确理解

    粗略来讲,__name__是当前模块,当模块被直接运行时模块名为__main__.这句话的意思是,当模块被直接执行时,代码将运行,当模块是被导入时,代码不被运行 例如,执行one.py # file ...

  5. HDFS Federation(转HDFS Federation(HDFS 联盟)介绍 CSDN)

    转载地址:http://blog.csdn.net/strongerbit/article/details/7013221 HDFS Federation(HDFS 联盟)介绍 1. 当前HDFS架构 ...

  6. java基础知识(初学)

    (小记) 文本文档方式可以下载notepad 在设置-新建-修改默认语言为java 编码为ANSI! java关键字特点:1.完全小写字母.如:public. java标识符:方法的名称,类的名称,变 ...

  7. Vue--- 一点车项目 连接数据库

    Vue--- 一点车项目  连接数据库 创建连接数据库配置 ###导入 const Koa = require('koa'); const Router = require('koa-router') ...

  8. windows搭建本地IIS服务器+php安装+移动设备内网访问服务器

    启动IIS服务 1. 打开 “控制面板” => "程序" => "启用或关闭Window功能": 2. 接着勾选相应设置: 3. 继续勾选对应目录下 ...

  9. youku客户端

    文件结构 config import os IP_PORT = ('127.0.0.1',8080) BASE_DIR = os.path.dirname(os.path.dirname(__file ...

  10. 【读书笔记 - Effective Java】03. 用私有构造器或者枚举类型强化Singleton属性

    实现Singleton(代表本质上唯一的系统组件)的三种方法: 1. 保持私有构造器,导出公有的静态成员,客户端访问该类的唯一实例. 2. 保持私有构造器,公有的成员是静态工厂方法. 3. 单元素的枚 ...