Arbitrage

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

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
 
套汇问题,floyd变形
 
/*
ID: LinKArftc
PROG: 1217.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
int n, m;
double table[maxn][maxn]; void floyd() {
for (int k = ; k <= n; k ++) {
for (int i = ; i <= n; i ++) {
for (int j = ; j <= n; j ++) {
table[i][j] = max(table[i][j], table[i][k] * table[k][j]);
}
}
}
} int main() {
//input;
int _t = ;
while (~scanf("%d", &n) && n) {
map <string, int> mp;
string str, str1;
int cnt = ;
for (int i = ; i <= n; i ++) {
cin >> str;
mp[str] = cnt ++;
}
memset(table, , sizeof(table));
scanf("%d", &m);
double change;
for (int i = ; i <= m; i ++) {
cin >> str >> change >> str1;
table[mp[str]][mp[str1]] = change;
}
floyd();
bool flag = false;
for (int i = ; i <= n; i ++) {
if (table[i][i] - 1.0 > eps) {
flag = true;
break;
}
}
if (flag) printf("Case %d: Yes\n", _t ++);
else printf("Case %d: No\n", _t ++);
} return ;
}

HDU1217 (Floyd简单变形)的更多相关文章

  1. POJ2240——Arbitrage(Floyd算法变形)

    Arbitrage DescriptionArbitrage is the use of discrepancies in currency exchange rates to transform o ...

  2. POJ 1125 Stockbroker Grapevine【floyd简单应用】

    链接: http://poj.org/problem?id=1125 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  3. Uvaoj 10048 - Audiophobia(Floyd算法变形)

    1 /* 题目大意: 从一个点到达另一个点有多条路径,求这多条路经中最大噪音值的最小值! . 思路:最多有100个点,然后又是多次查询,想都不用想,Floyd算法走起! */ #include< ...

  4. hdu1217 floyd

    floyd一遍即可.如果floyd后值有变大就是 #include<map> #include<string> #include<stdio.h> #include ...

  5. HDU1248 (完全背包简单变形)

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~

    B. Mr. Kitayuta's Colorful Graph ->  Link  <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...

  7. UVA 562 Dividing coins 分硬币(01背包,简单变形)

    题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...

  8. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  9. 实现了一个简单的cage变形器

    今天实现了一个简单变形器,可以用一个网格的形状影响另一个网格的形状. 如图,蓝色网格的形状被灰色网格操控. 当前的算法非常简单,就是计算蓝色网格每个点到灰色网格每个点的距离,以距离x次方的倒数作为权重 ...

随机推荐

  1. SQL - SELECT COUNT用法

     SQL Server数据库  COUNT() 函数返回匹配指定条件的行数.   语法   SQL COUNT(column_name) 语法   COUNT(column_name) 函数返回指定列 ...

  2. coreos install hpssacli

    基于官方的coreos ramdisk安装hp raid管理工具,其版本为debian8 apt-get install curl nano /etc/apt/sources.list deb htt ...

  3. MySQL训练营01

    一.数据库基础知识: 1. 数据库(database):保存有组织的数据的容器(通常是一个或者一组文件) 2. 数据库管理系统(DBMS):数据库软件,外界通过DBMS来创建和操纵数据库,具体是什么, ...

  4. python进阶训练

    1.列表,字典,集合解析 from random import randint #列表解析,选出大于0的元素 data=[randint(-10,10)for i in range(10)] resu ...

  5. http请求整理

    终于又回来了,先来简单整理一波http请求的信息.对于前端来说,不管是在面试还是在实际项目中,都有必要去了解一些关于http的信息. http请求包含三部分:请求行request line.请求头re ...

  6. ps学习笔记(二)

    1)选择所有图层: Ctrl+Alt+A2)查找层:ctrl+alt+shift+f,需要在层面板输入查找层名,可自动查找层:3)隔离层:可将选择图层,更改为隔离,只对选择的层编辑:注:图层面板中有一 ...

  7. Spring 集成Quartz

    在使用jdk的timer时发现无法在指定的日期进行执行任务.这便引入一个优秀的开源任务调度框架“quartz”.这里使用的是quartz-1.8.6版本.Quart的官网:http://www.qua ...

  8. [HDU6304][数学] Chiaki Sequence Revisited-杭电多校2018第一场G

    [HDU6304][数学] Chiaki Sequence Revisited -杭电多校2018第一场G 题目描述 现在抛给你一个数列\(A\) \[ a_n=\begin{cases}1 & ...

  9. Java学习全攻略-->阅读官方文档

    一直感觉Java的官方文档有些杂乱,最近特意整理了一下,仅供参考. 入口 Oracle官方文档入口:http://docs.oracle.com/.下级页面这边只整理了JavaEE跟JavaSE的文档 ...

  10. hdu 6057 Kanade's convolution(子集卷积)

    题解: 然后就是接下来如何fwt 也就是如何处理bit(x) - bit(y) = bit(k)这个条件. 其实就是子集卷积. 把bit(x)和bit(y)划分成两个集合,然后就是子集卷积的形式. 这 ...