zoukankan      html  css  js  c++  java
  • zoj1092 poj2240

    Arbitrage

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 Specification

    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 Specification

    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











    #include <cstdio> #include <cstring> #define maxn 50 //顶点数最大值 #define maxm 1000 //边数最大值 #define max(a,b) ( (a) > (b) ? (a) : (b) ) struct exchange //汇率关系 { int ci, cj; double cij; }ex[maxm]; //汇率关系数组 int i, j, k; //循环变量 int n, m; //货币种类的数目、汇率的数目 char name[maxn][20], a[20], b[20]; //货币名称 double x; //读入的汇率 double maxdist[maxn]; //源点i到其他每个顶点(包括它本身)的最长路径长度 int flag; //是否存在套汇的标志,flag=1表示存在 int kase = 0; //测试数据序号 int readcase( ) //读入数据 { scanf( "%d", &n ); if( n==0 ) return 0; for( i=0; i<n; i++ ) //读入n个货币名称 scanf( "%s", name[i] ); scanf( "%d", &m ); for( i=0; i<m; i++ ) //读入汇率 { scanf( "%s %lf %s", a, &x, b ); for( j=0; strcmp( a, name[j] ); j++ ) ; for( k=0; strcmp( b, name[k] ); k++ ) ; ex[i].ci = j; ex[i].cij = x; ex[i].cj = k; } return 1; } //Bellman-Ford算法:以顶点v0为源点,求它到每个顶点(包含它本身)的最大距离 void bellman( int v0 ) { flag = 0; memset( maxdist, 0, sizeof(maxdist) ); //初始化maxdis[]数组 maxdist[v0] = 1; for( k=1; k<=n; k++ ) //从maxdist(0)递推到maxdist(1),...,maxdist(n) { for( i=0; i<m; i++ ) //判断每条边,加入它是否能使得最大距离增加 { if( maxdist[ex[i].ci]*ex[i].cij > maxdist[ex[i].cj] ) { maxdist[ex[i].cj] = maxdist[ex[i].ci]*ex[i].cij; } } } if( maxdist[v0]>1.0 ) flag = 1; } int main( ) { while( readcase( ) ) //读入货币种类的数目 { for( i=0; i<n; i++ ) { bellman( i ); //从第i个顶点出发求最长路径 if( flag ) break; } if( flag ) printf( "Case %d: Yes ", ++kase ); else printf( "Case %d: No ", ++kase ); } return 0; }
  • 相关阅读:
    分布式框架服务调用超时设计与实现
    分布式框架灰度发布实现
    写给自己的 SOA 和 RPC 理解
    Java 基础【19】代理
    Java 基础【18】 反射与内省
    Spring HttpInvoker 从实战到源码追溯
    Lombok 使用攻略
    Guava Preconditions 工具参数前置校验
    CAS 单点登录【2】自定义用户验证
    Hibernate 离线对象构建通用查询
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3213909.html
Copyright © 2011-2022 走看看