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. Windows 2008 Scheduled tasks result codes

    0 or 0x0: The operation completed successfully. 1 or 0x1: Incorrect function called or unknown funct ...

  2. ETL测试小结

    一.ETL测试的重要性: ETL(Extract-Transform-Load的缩写,即数据抽取.转换.装载的过程)作为BI/DW(Business Intelligence)的核心和灵魂,能够按照统 ...

  3. Tuple和 ValueTuple

    这个类型还是学习C#7.0的语法在看到的,这边单独拿来学习下. 学习地址: https://docs.microsoft.com/zh-cn/dotnet/csharp/tuples https:// ...

  4. 系统优化怎么做-JVM优化之开篇

    大家好,这里是「聊聊系统优化 」,并在下列地址同步更新 博客园:http://www.cnblogs.com/changsong/ 知乎专栏:https://zhuanlan.zhihu.com/yo ...

  5. 协议类接口 - SPI

     一.SPI概述 SPI(Serial Peripheral Interface,串行外设接口)总线系统是一种同步串行外设接口,它可以使CPU与各种外围设备以串行方式进行通信以交换信息.一般主控SoC ...

  6. 算法是什么(二)手写个链表(java)

    算法是什么(二)手写个链表(java)   liuyuhang原创,未经允许禁止转载 目录 算法是什么(〇) 很多语言的API中都提供了链表实现,或者扩展库中实现了链表. 但是更多的情况下,Map(或 ...

  7. ArrayList的去重问题

    面试被问及arraylist的去重问题,现将自己想的两种解决方案写在下面 /** * Description: * ClassName:Uniq * Package:com.syd.interview ...

  8. Servlet Struts2 SpringMVC 获取参数与导出数据 方法比较

    servlet中通过request.getParameter()从网页获取参数 通过request session servletContext几个域对象的setAttribute(String ,O ...

  9. 工具 | Axure基础操作 No.4

    昨天因为有事没有学习,很愧疚,今天赶紧补上.俗话说,"学如逆水行舟,不进则退"还是很有道理的. 1.设置页面内容格式 这里主要是在浏览器中的内容所出现的对齐格式,左对齐或者是居中对 ...

  10. 『ACM C++』 Codeforces | 1005D - Polycarp and Div 3

    今天佛了,魔鬼周一,在线教学,有点小累,但还好,今天AC了一道,每日一道,还好达成目标,还以为今天完不成了,最近任务越来越多,如何高效完成该好好思考一下了~最重要的还是学业的复习和预习. 今日兴趣新闻 ...