E - Arbitrage

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2015-11-24)

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 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

先输入货币的种类,然后接着是每个货币的兑换关系,问是否有一种货币能增值,也就是通过某种兑换关系是g[i][i] >1

初始化傻逼地将g设成了INF,白白贡献了4次WA

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
const int MAX = ;
const int INF = << ;
int n;
double g[MAX][MAX];
map<string,int> m;
void Floyd()
{
for(int k = ; k <= n; k++)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(g[i][k] != && g[k][j] != && g[i][j] < g[i][k]*g[k][j])
{
g[i][j] = g[i][k]*g[k][j];
}
}
}
}
}
int main()
{
int num = ;
while(scanf("%d", &n) != EOF && n)
{
int t;
char temp[],str[];
double rat;
m.clear();
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
g[i][j] = ;
}
for(int i = ; i <= n; i++)
{
scanf("%s", temp);
m[temp] = i;
}
scanf("%d", &t);
for(int i = ; i <= t; i++)
{
scanf("%s%lf%s",temp,&rat,str);
g[ m[temp] ][ m[str] ] = rat;
}
Floyd();
int flag = ;
for(int i = ; i <= n; i++)
{
if(g[i][i] != && g[i][i] > 1.0)
{
flag = ;
break;
}
}
printf("Case %d: ",++num);
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return ;
}
 

POJ 2240Arbitrage(Floyd)的更多相关文章

  1. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  2. Stockbroker Grapevine - poj 1125 (Floyd算法)

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30454   Accepted: 16659 Description S ...

  3. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  4. Stockbroker Grapevine(floyd)

    http://poj.org/problem?id=1125 题意: 首先,题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时, 输入数据结束),然后接下来N行描述第i(1< ...

  5. (floyd)佛洛伊德算法

    Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...

  6. [CodeForces - 296D]Greg and Graph(floyd)

    Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...

  7. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  8. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

  9. Radar Installation POJ - 1328(贪心)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

随机推荐

  1. 工作流模式与K2实现--(2)

      结构化过程  这两个模式的共同点在于:模式所涉及流程的执行路径是由运行时决定的,而非设计时确定.包括:Arbitrary cycles(强制循环模式) .Implicit termination( ...

  2. C语言 const常量讲解

    //const的本质 //const本质上是伪常量,无法用于数组初始化以及全局变量初始化 //原因在于const仅仅限定变量无法直接赋值,但是却可以通过指针间接赋值 //例如局部常量在栈区,而不在静态 ...

  3. jquery 获取select框选中的值示例一则

    $('#MODULE_TYPE').change(function(){ var moduleType=$(this).children('option:selected').val();//这就是s ...

  4. C#中小数点后保留两位小数,四舍五入的函数及使用方法

    Math.Round(45.367,2)     //Returns   45.37 Math.Round(45.365,2)     //Returns   45.36 C#中的Round()不是我 ...

  5. Android开发探秘之二:导入存在的项目及其注意事项

    网上看到有jsoup写的例子,就下载下来进行了研究,但是发现不会导入,于是就百度一下,发现了方法:也就是依次点击“File”->“Import”->“General”->“Exist ...

  6. 20145314郑凯杰《信息安全系统设计基础》GDB调试32位汇编堆栈分析

    20145314郑凯杰<信息安全系统设计基础>GDB调试32位汇编堆栈分析 本篇博客将对第五周博客中的GDB调试32位汇编堆栈进行分析 首先放上以前环境配置的图: 图1: 测试代码: #i ...

  7. WIN7下USB多点触摸,一次发多个数据包的延迟问题,重要!

    这个问题很常见, 花了差不多一个星期时间来解决.硬件相关的东西太多坑了,而且这些坑不像代码那样可见.   使用混合模式,每次最多报告2个点.如果是5点则需要上报三次. 问题就来了,atmel的CTP最 ...

  8. [BZOJ 2456]Mode(神奇的抵销)

    题意:求一串数的众数(保证次数的出现次数超过一半),n<=500000 很简单是不是……快拍一下不就行了吗,不超时呢=W=,不过……尼玛空间只有1M…… 于是此题就成了神题(理解成智商题= =本 ...

  9. C#基础知识系列六(静态类和静态类成员)

    静态类 静态类与非静态类基本相同,但存在一个区别:静态类不能实例化. 也就是说,不能使用 new 关键字创建静态类类型的变量. 因为没有实例变量,所以要使用类名本身访问静态类的成员. 例如,如果名为 ...

  10. Javascript基础系列之(五)关键字和保留字 (keyword)

    关键字不可以作为变量名或者函数名 break case catch continue default delete do else finally for function if in instanc ...