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. 七天入门C++

  2. 九度OJ--Q1168

    import java.util.Scanner; public class q1168 { public static void main(String[] args) { Scanner scan ...

  3. php+Mysql分页 类和引用详解

    一下内容为专用于分页的类以及具体的方法和解析.<?php class Page { private $total; //数据表中总记录数 private $listRows; //每页显示行数 ...

  4. Android Studio的初体验

    在机缘巧合之下遇到了安卓开发,接触了Android Studio开始了漫长的改bug的道路,以下为简易版心酸历程 首先我需要成功安装Android Studio,由于我过于叛逆以及为了避免出错于是从一 ...

  5. 《学习OpenCV》课后习题解答9

    题目:(P126) 创建一个程序,使其读入并显示一幅图像.当用户鼠标点击图像时,获取图像对应像素的颜色值(BGR),并在图像上点击鼠标处用文本将颜色值显示出来. 解答: 本题关键是会用cvGet2D获 ...

  6. yii视频地址哦

    https://www.yiichina.com/video  

  7. Aspose.Pdf合并PDF文件

    使用Aspose.Pdf类库,有很多种方法可以合并PDF文件,这里简单介绍小生见到的几种: Doucment.Pages.Add PdfFileEditor.Append PdfFileEditor. ...

  8. 搭建Lepus数据库监控系统

    一.  安装环境 系统环境:centos6.5 IP:192.168.30.242 hostname:vpn.org 软件:LAMP均已安装.(请确保这些正常安装,并能使用). 系统核心包:(摘自官方 ...

  9. 【bzoj4998】星球联盟 LCT+并查集

    题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...

  10. BZOJ4481 JSOI2015非诚勿扰(概率期望+树状数组)

    首先求出每个女性接受某个男性的概率.这个概率显然是一个无穷等比数列求和. 然后按编号从小到大考虑每个女性,维护出每个男性被选择的期望次数,BIT上查询后缀和即可. 需要long double. #in ...