poj 1860 Currency Exchange (spfa或Bellman-Ford)

Description

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 – 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B – numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA – exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.
Input

The first line of the input contains four numbers: N – the number of currencies, M – the number of exchange points, S – the number of currency Nick has and V – the quantity of currency units he has. The following M lines contain 6 numbers each – the description of the corresponding exchange point – in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.
Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

Sample Input

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

Sample Output

YES

大致题意:有多组数据,每组数据首先输入n,m,s,v。n表示有n个货币兑换点,m表示有m组兑换关系,兑点A到兑点B的汇率和手续费可能与兑点B到兑点A的汇率和手续费不同,是双向的。s表示源点,v表示初始金额。接下来的m行每行输入a,b,rab,cad,rba,cba ,表示兑换点a和兑换点b之间的汇率和手续费。问源点s经过一系列的兑换货币后回到源点s时的货币是否能增加,如果能则输出yes,否则输出no。

思路:初始化dis(S)=V 而源点到其他点的距离(权值)初始化为无穷小(0),当s到其他某点的距离能不断变大时,说明存在最大路径;如果可以一直变大,说明存在正环。判断是否存在环路,用Bellman-Ford和spfa都可以。
a点到b点的权值为(v’-cab)*rab b点到a点的权值为(v”-cba)*rba

代码如下

1.spfa

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;

const int N=110;
int n,m,s;
double dis[N],rate[N][N],cost[N][N];
double v;

bool spfa(int start)
{
    bool vis[N];
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    dis[start]=v;
    queue<int> que;
    que.push(start);
    vis[start]=true;
    while(!que.empty())
    {
        int x=que.front();
        que.pop();
        vis[x]=false;
        for(int i=0;i<=n;i++)
        {
            if(dis[i]<(dis[x]-cost[x][i])*rate[x][i])
            {
                dis[i]=(dis[x]-cost[x][i])*rate[x][i];
                if(dis[start]>v)
                return true;
                if(!vis[i])
                {
                    que.push(i);
                    vis[i]=true;
                }
            }
        }
    }
    return false;
}

void init()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            if(i==j)
            rate[i][j]=1;
            else 
            rate[i][j]=0;

            cost[i][j]=0;    
        }
}
 int main()
 {
    while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
    {
        int a,b;
        double rab,rba,cab,cba;
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
            rate[a][b]=rab;
            rate[b][a]=rba;
            cost[a][b]=cab;
            cost[b][a]=cba;
        }
        if(spfa(s))
            printf("YES\n");        
        else 
            printf("NO\n");
    }
   return 0;
}

2.Bellman_Ford

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;

const int N=110;
int n,m,s;
double dis[N];
double v;
int k;
struct node
{
    int from,to;
    double rate,cost;
}p[N];


bool Bellman_Ford() 
{
    memset(dis,0,sizeof(dis));
    dis[s]=v;
    for(int i=1;i<=n-1;i++)
    {
        bool flag=false;
        for(int j=0;j<k;j++)
        {
            int a=p[j].from,b=p[j].to;
            double r=p[j].rate,c=p[j].cost;
            if(dis[b]<(dis[a]-c)*r)
            {
                dis[b]=(dis[a]-c)*r;
                flag=true;
            }
        }
        if(!flag)
        break;
    }
    for(int i=0;i<k;i++)
    if(dis[p[i].to]<(dis[p[i].from]-p[i].cost)*p[i].rate)
    return true;

    return false;
}
 int main()
 {
    while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
    {
        int a,b;
        double rab,rba,cab,cba;

        k=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
            p[k].from=a;
            p[k].to=b;
            p[k].rate=rab;
            p[k++].cost=cab;
            p[k].from=b;
            p[k].to=a;
            p[k].rate=rba;
            p[k++].cost=cba;
        }
        if(Bellman_Ford()) 
            printf("YES\n");        
        else 
            printf("NO\n");
    }
   return 0;
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/elbadaernu/article/details/70832843
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞