poj2240——Arbitrage(Bellman-Ford算法)

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

和poj1860类似的题,找权值为正的环路。就是节点的处理有些技巧,我这里用了哈希表

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 5005
#define mod 1000000007
using namespace std;
map<string,int> mp;
int n,m,e;
double dis[MAXN];
struct Node
{
    int beg,end;
    double r;
};
Node edge[MAXN<<1];
void add(int b,int en,double r)
{
    edge[e].beg=b;
    edge[e].end=en;
    edge[e].r=r;
    e++;
}
bool relax(int p)
{
    double t=dis[edge[p].beg]*edge[p].r;
    if(dis[edge[p].end]<t)
    {
        dis[edge[p].end]=t;
        return true;
    }
    return false;
}
bool bellman()
{
    memset(dis,0,sizeof(dis));
    dis[0]=1;
    for(int i=1; i<n; ++i)
    {
        bool flag=false;
        for(int j=0; j<e; ++j)
        {
            if(relax(j))
                flag=true;
        }
        if(dis[0]>1)
            return true;
        if(!flag)
            return false;
    }
    for(int j=0; j<e; ++j)
        if(relax(j))
            return true;
    return false;
}
int main()
{
    int cnt=1;
    while(~scanf("%d",&n)&&n)
    {
        string a,b;
        for(int i=0; i<n; ++i)
        {
            cin>>a;
            mp[a]=i;
        }
        scanf("%d",&m);
        double r;
        e=0;
        while(m--)
        {
            cin>>a>>r>>b;
            add(mp[a],mp[b],r);
        }
        if(bellman())
            printf("Case %d: Yes\n",cnt++);
        else
            printf("Case %d: No\n",cnt++);
    }
    return 0;
}
时间: 2024-10-14 16:47:16

poj2240——Arbitrage(Bellman-Ford算法)的相关文章

Bellman - Ford 算法解决最短路径问题

Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力不从心了,而Bellman - Ford算法可以解决这种问题. Bellman - Ford 算法可以处理路径权值为负数时的单源最短路径问题.设想可以从图中找到一个环路且这个环路中所有路径的权值之和为负.那么通过这个环路,环路中任意两点的最短路径就可以无穷小下去.如果不处理这个负环路,程序就会永远运

POJ-2240 -Arbitrage(Bellman)

题目链接:Arbitrage 让这题坑了,精度损失的厉害,用赋值的话,直接全部变成0.00了,无奈下,我只好往里输了,和POJ1860一样找正环,代码也差不多,稍微改改就可以了,但是这个题精度损失的比那个....水过 POJ计划的最短路模块,刷完了,最短路问题,挺坑的,但是就是那点东西,变来变去,就是改改dis[]的更新条件. 明天就要开始POJ的最小生成树了, ME                  TI 704Kb            46Ms #include <iostream> #

Bellman—Ford算法思想

---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G运行Bellman—Ford算法的结果是一个布尔值,表明图中是否存在着一个从源点s可达的负权回路.若存在负权回路,单源点最短路径问题无解:若不存在这样的回路,算法将给出从源点s到图G的任意顶点v的最短路径值d[v] Bellman—Ford算法流程 分为三个阶段: (1)初始化:将除源点外的所有顶点

poj 3259 Wormholes (BELLman—FOrd算法)(邻接矩阵表示)

http://poj.org/problem?id=3259 之前一开始 ,没做出来,搁置了好几天才看见bug所在.所以今天a掉了 ,把代码贴出来,这个使用邻接矩阵表示的 ,下一篇用邻接表可以提高效率. #include<iostream> #include<queue> #include<stdio.h> #include<string.h> using namespace std; const int INF=600; int G[INF][INF];

最短路径——Bellman Ford算法(C++)

源代码: #include<cstdio>#include<cstring>int m(1),n,k,i[1001],x[1001],y[1001],f[1001];int main(){ scanf("%d%d",&n,&k); for (int a=1;a<=n;a++) for (int b=1;b<=n;b++) { scanf("%d",&i[m]); if (i[m]!=-1) { x[m]=a

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22405   Accepted: 8095 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

bellman-ford算法

给定图G(V, E)(其中V.E分别为图G的顶点集与边集),源点s, 数组Distant[i]记录从源点s到顶点i的路径长度,初始化数组Distant[n]为, Distant[s]为0: 以下操作循环执行至多n-1次,n为顶点数:对于每一条边e(u, v),如果Distant[u] + w(u, v) < Distant[v],则另Distant[v] = Distant[u]+w(u, v).w(u, v)为边e(u,v)的权值:若上述操作没有对Distant进行更新,说明最短路径已经查找完

POJ2240——Arbitrage(Floyd算法变形)

Arbitrage DescriptionArbitrage 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

poj-------(2240)Arbitrage(最短路)

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