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. Qt Qwdget 汽车仪表知识点拆解7 图像绘制,旋转

    先贴上效果图,注意,没有写逻辑,都是乱动的 看下最中心的指针旋转,这里使用的QPainter的绘制函数 要显示复杂的效果,需要分层 void Widget::draw_number_pointer() ...

  2. CPU拓扑结构

    本篇旨在认识一下以下三种CPU拓扑结构分别是什么: Symmetric multiprocessing (SMP) Non-uniform memory access (NUMA) Simultane ...

  3. C语言运算符(注意事项)

    1.C语言取余注意事项:%   a.求余.模运算符(%)时要求两数必须是整型数据. b.取余的结果,是取决于被除数   (不管除数是正数 还是 负数,模的符号与被除数的符号相同).   例:8÷2=4 ...

  4. LeetCode 19——删除链表的倒数第 N 个节点

    1. 题目 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后 ...

  5. Week2 Teamework from Z.XML 软件分析与用户需求调查(四)Bing桌面及助手的现状与发展

    一.Bing搜索的相关背景 第一,必应搜索前几年的发展重点在于欧美市场,并且取得了一定的成效:根据 Hitwise 的统计数据,Bing 在 2011年3 月份市场占有率突破了 30% 大关,达到 3 ...

  6. Week3 Teamework from Z.XML-团队分工及贡献分分配办法

    引言:团队项目即将开展,本文将就团队分工,以及分数分配办法进行阐述 一.团队分工 本周我们团队进行了初步的分工,结果如下: PM: 李孟 Dev:毛宇 薛亚杰 肖俊鹏 罗凡 Test:周敏轩 马辰 李 ...

  7. android桌面悬浮窗仿QQ手机管家加速效果

    主要还是用到了WindowManager对桌面悬浮进行管理. 需要一个火箭的悬浮窗 一个发射台悬浮窗  ,判断火箭是否放到了发射台,如果放上了,则使用AsyTask 慢慢将火箭的图片往上移.结束后., ...

  8. 一个android控件资源网站

    http://www.androidviews.net/ 里面有各种常用控件,赞~

  9. homework for Java

    public class Dog { private String name; private String color; private int age; public Dog(String nam ...

  10. 用js通过url传参把数据从一个页面传到另一个页面

    好长时间没写博客了,时值五一,外面到处人山人海,本宝宝还是好好呆在家学习吧.好了,言归正传.在没有后台支持的情况下,如何实现从一个页面像另一个页面来传递数据呢?应该很多人遇到过这个问题吧.那我就来说说 ...