POJ 2240 Bellman_Ford

Arbitrage

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 16429 Accepted: 6909

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

Source

Ulm Local 1996

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
double cost[32][32];
double dis[32];
int n,m;
map<string,int>mapstring;
bool Bellman_Ford()
{
     for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
          for(int j=1;j<=n;j++)
     {
          if(cost[i][j]<cost[i][k]*cost[k][j]) cost[i][j]=cost[i][k]*cost[k][j];
     }
     for(int i=1;i<=n;i++)
        if(cost[i][i]>1) return true;
     return false;
}
int main()
{
   // freopen("in.txt","r",stdin);
    string s,s1,s2;
    double c;int cnt=1;
    while(1){
        scanf("%d",&n);
        if(n==0) break;
        for(int i=1;i<=n;i++)
        {
            cost[i][i]=1;
            cin>>s;
           mapstring[s]=i;
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            cin>>s1>>c>>s2;
            cost[mapstring[s1]][mapstring[s2]]=c;
        }
        int ans=Bellman_Ford();
        if(ans) printf("Case %d: Yes\n",cnt++);
        else printf("Case %d: No\n",cnt++);
    }
    return 0;
}
时间: 2024-10-13 18:21:19

POJ 2240 Bellman_Ford的相关文章

poj 2240 Bellman-Flod 求环

http://poj.org/problem?id=2240 深刻体现了自己代码能力有问题外加改模板能力有问题,外加Debug有问题.以后做到: 1.算法原理可以轻易弄出来, 2.代码模板自己收集各种用法,以及已经的做过的改变的方法: 3.没有完整清晰的思路不敲代码: 4.在Debug时没有基本绝对的把握,不点击"编译+运行",不乱试 回到这道题: 我主要是想把Bellman-Ford的模板改为链式前向星的,然后就各种悲剧调试...... 学到三点:1.比例的初始化,求最大,源1.0,

poj 2240

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14832   Accepted: 6255 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 currenc

POJ 2240 Arbitrage

Bellman 求最大环. 询问货币是否纯在套汇. 假如给你 1 元,通过兑换之后 超过 1 元就是存在套汇了. 用 map 映射比较方便. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<queue> #include<map> #include<iostream>

poj 2240 Arbitrage 题解

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 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 currenc

poj 2240 Arbitrage (Floyd)

链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 BritishPound. 问在这N种货币中是否存在货币经过若干次兑换后,兑换成原来的货币能够使货币量添加. 思路:本题事实上是Floyd的变形.将变换率作为构成图的路径的权值.只是构成的图是一个有向图. 最后将松弛操作变换为:if(dis[i][j]<dis[i][k]*dis[k][j]). #includ

POJ 2240 -- Arbitrage(Bellman-Ford)

POJ 2240 -- Arbitrage(Bellman-Ford) 题意: 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. Bellman-ford 算法: 一个具有n个顶点的图如果不存在环,则从顶点x,到顶点y,最多经过n-1条边(要考虑连通性,每个顶点最多经过 1 次),因此 x 到 y 的最短路 最多经过 n - 1 次松弛操作(就是更新长度)就应该出现,如果第 n 次松弛还可以得到最优,那么这个图就肯定是存在环了(直接用Dijkstra 就无法得到最优的,环

POJ 2240 Arbitrage Bellman_ford 判读是否存在正环

和POJ1860差不多,就是用bellmanford判读是否存在正环,注意的是同种货币之间也可以交换,就是说:A货币换A货币汇率是2的情况也是存在的. #include<stdio.h> #include<string.h> #include<cstring> #include<string> #include<math.h> #include<queue> #include<algorithm> #include<

Arbitrage POJ 2240

http://poj.org/problem?id=2240 题意:现你有N种货币,在进行货币进行兑换后,最终仍需兑换成原本货币,若其中某一种货币增加了,就输出“Yes”,否则输出“No”. 一开始TLE了, 因为 return 1 的位置放错了.. 一开始没咋想,直接放到最后判断了... #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include

poj 2240 Arbitrage(bellman-ford 判断正环)

http://poj.org/problem?id=2240 基本和poj 1860相同 只是把单点变成了任意点 做完1860再做这题就完全把思路套上就过了 做完才发现网上的题解都用的是floyd 不过整体思路都是大同小异吧 不过在效率上好像就低下了太多= = #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<stack> #include<