Arbitrage

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

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
 
Source
 
 
         
只能感叹外国就连ACM比赛都是这么符合实际且实用啊。。。。。
题意:
        意思就是叫你算出银行的汇率,判断是否可以赚钱。。哈哈,当然这是非专业的。
        其实题目是告诉我们几个国家之间的汇率,然后叫我们判断他们之间是否有环。且最终通过这种转换能否赚钱。
 其实就是一个有关DP问题于是运用暴力的floyd就可以ko了。

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstring>
using namespace std; double dp[31][31];
bool Floyd(int n)
{
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
dp[i][j] = max(dp[i][j],dp[i][k]*dp[k][j]);
for(int i = 1;i <= n;i++)
if(dp[i][i] > 1)
return true;
return false;
} int main()
{
double rate;
int n,m,i,kase = 1;
while(cin>>n,n)
{
string s,a,b;
map<string,int> mp;
memset(dp,0,sizeof(dp));
for(i = 1;i <= n;i++)
{
cin>>s;
mp[s] = i;
}
cin>>m;
for(i = 1;i <= m;i++)
{
cin>>a>>rate>>b;
dp[mp[a]][mp[b]] = rate;
}
if(Floyd(n))
printf("Case %d: Yes\n",kase++);
else
printf("Case %d: No\n",kase++);
}
return 0;
}

Arbitrage HDU的更多相关文章

  1. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  2. HDU 1217 Arbitrage (Floyd)

    Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...

  3. hdu 1217 Arbitrage (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217 /************************************************* ...

  4. HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...

  5. hdu 1217 Arbitrage (spfa算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...

  6. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

  7. hdu 1217 Arbitrage(佛洛依德)

    Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. hdu 1217 Arbitrage

    Flody多源最短路 #include<cstdio> #include<cstring> #include<string> #include<cmath&g ...

  9. HDU 1217 Arbitrage(Floyd的应用)

    给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include< ...

随机推荐

  1. checkbox的问题整理

    之前做过一些checkbox的小功能,由于水平有限总是在这一部分花费一些时间,趁现在时间充裕些,整理出来. checkbox是否被选中的判断 第一种: $(this).is(":checke ...

  2. 练习使用css3实现3d按钮

    网上有很多漂亮的用css3实现的3d按钮,如'这个'.‘糖果色按钮’, 今天练习了一下,喏,下面这样,兼容性不好. 小黑子小虎子 3d效果和发光效果都是利用box-shadow,也经常利用伪元素:af ...

  3. 3月22日 html(三)css样式表

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控制精确,但是可重用性差,冗余较多. 例 ...

  4. MySQL 学习笔记 (limit offset)

    select * from table limit (10000,10);这样是很慢的,因为要定位 比较快的写法是 select * from table where id >=(select ...

  5. About Adultism and why things ar the way they are

    About - Adultism About Adultism and why things ar the way they are In this page we will try to clari ...

  6. javascript笔记6之函数

    /* function box() { //函数的声明 alert('我只有被调用才可以执行!'); //函数本身没有运行功能 } //必须调用才可以执行 box(); //调用 function b ...

  7. <php>添加数据注意事项

    如果报错信息里有:fetch_all(),肯定是sql语句写错 get传值:<a href="chuli.php?name=1&code=2">处理</a ...

  8. JavaScript进阶篇 - -第1章 系好安全带

    第1章 系好安全带 html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", ...

  9. 禁用menu键

    发现很多应用中基本不再使用menu键来显示菜单了,而在android studio中创建一个activity时,默认使用了actionBar,就是在右上角会显示三个点的内容,点击会出现Settings ...

  10. python3-day4(递归)

    递归 特点 递归算法是一种直接或者间接地调用自身算法的过程.在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于理解. 递归算法解决问题的特点: (1) 递归就是在 ...