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. WPF技巧-Canvas转为位图

    转自:http://www.cnblogs.com/tmywu/archive/2010/09/14/1825650.html 在WPF中我们可以将Canvas当成一种画布,将Canvas中的控件当成 ...

  2. log4j输出日志乱码(转)

    log4j日志文件乱码问题的解决方法 log4j日志文件中文乱码处理方法 log4j 控制台和文件输出乱码问题解决 写在前面,第三篇文章中将原因解释的最清楚,为什么设置为UTF-8或者GBK就生效了, ...

  3. 使用Windows Live Writer发布日志

    前言 Windows Live Writer是非常不错的一个日志发布工具,支持本地写文章,然后通过点击一个按钮就发布到网站上,如果借助插件,还可以同时发布到多个博客网站,功能非常强大,很多博友认识她之 ...

  4. 信息安全系统设计基础实验一:Linux开发环境的配置和使用

    北京电子科技学院(BESTI) 实验报告 课程:信息安全系统设计基础    班级:1353 姓名:芦畅 傅冬菁 学号:20135308 20135311 成绩:       指导教师:娄家鹏      ...

  5. 【转】十分详细的xStream解析

    转自博文:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html xStream框架 xStream可以轻易的将Java对象和xml文 ...

  6. display:inline-block的坑

    一直用display:inline-block做某种导航栏还很爽,突然有一个柱状图的需求便也这么做了,于是成功被坑. 简简单单个需求,大致这样 只用几个li加上display:inline-block ...

  7. Mac无法找到摄像头问题解决

    facetime显示“未检测到摄像头”之类的,重启后可能摄像头有工作正常了,摄像头不稳定 重置 NVRAM后恢复正常,据说机器卡的时候,此法也可以使用. https://support.apple.c ...

  8. 安装VS2008无法更改安装路径解决方法

    一直用VS2012 以及 VS2012开发,但是他们都不支持Wince程序的开发,所有要安装VS2008.但是发现VS2008只能安装在C盘,要知道C空间很宝贵的. 经过查找资料发现系统中已经安装了V ...

  9. KMP和扩展KMP

    文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...

  10. 7、面向对象以及winform的简单运用(委托)

    委托 1.概念: 委托可以看成是一个函数的“容器”,将某一具体的函数“装入”后,就可以把它当成函数一样使用.委托变量可看成是一种类型安全的函数指针,它只能接收符合其要求的函数地址. 2.定义方法:de ...