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. [转]C#如何获取客户端IP地址

    代码如下: /// <summary> /// 获取客户端IP地址    /// </summary> /// <returns></returns> ...

  2. 【Node.js】新建一个NodeJS 4.X项目

    前提工作 1.安装Node.js 各种下一步就好 2.安装NPM(node package manager) 安装好Node.js之后,打开cmd,输入npm install npm -g,程序会自动 ...

  3. java多线程注意事项

    1:继承thread和实现Runnable创建线程的区别: 继承thread创建的对象直接start()就可以就绪,但是使用Runnable所new出来的对象要先new Thread(xx)才能sta ...

  4. php分页方法

    $page_on=15;//定义每页显示数 $pageNum=$_GET['pageNum']; //当前页数 $result = mysql_query("SELECT * FROM ne ...

  5. Java反射机制--是什么,为什么,怎么用。

    往往当我们面对一项新的知识时,我们往往需要知道三个方面,它是什么,它能做什么,它比原有知识强在哪里,我们该怎么使用它.当你能够解决这些问题时,便意味着你已经对这项知识入门了. 一.是什么 Java R ...

  6. 『C++』Temp_2018_12_06

    #include <iostream> #include <string> using namespace std; class Type{ public: string Na ...

  7. java web多组件协作实现用户登录验证

    实现步骤: 1.创建用户登录提交界面 2.创建处理用户登录请求servlet组件Main 3.创建代表登录成功响应的servlet的组件LoginSuccess 4.创建代表登录失败响应的servle ...

  8. 463. Island Perimeter (5月29日)

    解答 class Solution { public: int islandPerimeter(vector<vector<int>>& grid) { int num ...

  9. idea 引入多项目

    1.先导入总包 2.右侧mavenmaven,选择parent的pom.xml 3.右上角“Project Structure”检查SDK

  10. 用原生JS写一个网页版的2048小游戏(兼容移动端)

    这个游戏JS部分全都是用原生JS代码写的,加有少量的CSS3动画,并简单的兼容了一下移动端. 先看一下在线的demo:https://yuan-yiming.github.io/2048-online ...