Poj 2240 Arbitrage

click here~~

                                        ***Arbitrage***

Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 17969  Accepted: 7597 

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

题目大意:这是一个汇率兑换问题,就相当于n个点,m条边,然后就是知道从I到j的汇率,看能不能赚钱:
解题思路:就是看能不能兑换回来的时候权值是否增加就行了,采用Floyd算法。。。
上代码:

/*
2015 - 8 - 14 下午
Author: ITAK

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 100;
double map[maxn][maxn];
int n, m;
char str[maxn][maxn],s1[maxn],s2[maxn];
//map初始化
void Init()
{
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
            if(i == j)
                map[i][j] = 1;
            else
                map[i][i] = 0;
}
//Floyd 算法
void Floyd()
{
    int i, j, k;
    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(map[i][k]*map[k][j] > map[i][j])
                    map[i][j] = map[i][k]*map[k][j];
}
int main()
{
    int cas = 1;
    while(cin>>n,n)
    {
        Init();
        for(int i=1; i<=n; i++)
            cin>>str[i];
        cin>>m;
        double x;
        for(int i=1; i<=m; i++)
        {
            int j, k;
            cin>>s1>>x>>s2;
            for(j=1; j<=n; j++)
                if(strcmp(str[j],s1) == 0)
                    break;
            for(k=1; k<=n; k++)
                if(strcmp(str[k],s2) == 0)
                    break;
            map[j][k] = x;
        }
        Floyd();
        cout<<"Case "<<cas++<<":"<<' ';
        int f;
        for(f=1; f<=n; f++)
            if(map[f][f] > 1)
                break;
        if(f > n)
            puts("No");
        else
            puts("Yes");
    }
    return 0;
}
时间: 2024-11-02 14:41:17

Poj 2240 Arbitrage的相关文章

POJ 2240 Arbitrage:最短路 Floyd

Arbitrage:http://poj.org/problem?id=2240 大意: 给你m种货币,给你m种货币兑换规则,问通过这些规则最后能不能盈利.eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美元能换0.5*10*0.21=1.05美元,净赚0.05美元. 思路: 用Floyd找出每两种钱之间的最大兑换关系,遍历一遍,看有没有那种钱币最后能盈利,有就输出Yes,没有就是No.在处理钱币名称与编号之间的关系时,可以用map存(比较好用),当然也可以用字符串比较.

poj 2240 Arbitrage 最短路

    其实就是找到乘积最大的一条回路,如果大于1就是YES否则就是NO,用bellman N4都能过-- /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include

POJ 最短路问题题号汇总

求最短路基本的算法: 1>Dijkstra算法2>Bellman-Ford算法3>Floyd算法4>Floyd-Warshall算法5>Johnson算法6>A*算法 题目: 1.poj1062 昂贵的聘礼(中等)     此题是个经典题目:用Dijkstra即可:但是其中的等级处理需要一定的技巧:    要理解好那个等级制度:这个处理好,基本就是裸体Dijkstra: 2 poj1125 Stockbroker Grapevine(基本)    这个是简单Floyd,

poj系统训练计划

转载:http://www.cnblogs.com/silveryelf/archive/2011/10/29/2228681.html 初级: 基本算法: 枚举:1753    2965 贪心:1328    2109    2586 构造:3295 模拟:1068    2632    1573    2993    2996 图: 最短路径:1860    3259    1062    2253    1125    2240 最小生成树:1789    2485    1258    

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

POJ:DNA Sorting 特殊的排序

Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to

POJ 1001 Exponentiation 无限大数的指数乘法 题解

POJ做的很好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. 本题对程序健壮性的考查到达了变态级别了. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 某人贴出的测试用例数据地址: http://poj.org/showmessage?message_id=76017 有

POJ 1860 Currency Exchange:最短路 Bellman-Ford

Currency Exchange:http://poj.org/problem?id=1860 大意:有多种货币,之间可以交换,但是需要手续费,也就是说既有汇率又有手续费.问经过交换之后能不能赚. 思路:Bellman_Ford,因为要求最长路,所以松弛条件改一下就好了. Tips: 3              2                  1                20.0货币的数量   兑换点的数量     主人公拥有的货币量    主人公拥有货币的价值1 2 1.00

POJ 1258 Agri-Net:最小生成树 Prim 模版题

Agri-Net:http://poj.org/problem?id=1258 大意:新镇长竞选宣言就是将网络带到每一个农场,给出农场个数,两两之间建光缆的耗费,求所有都联通的最小耗费. 思路:最小生成树,因为边比较稠密,用Prim做. PS:对于比较稠密的图,用Prim,对于比较稀疏的图,用 Kruskal.Kruskal是找边的过程,稀疏的话会比较快. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/