zoukankan      html  css  js  c++  java
  • Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路

    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 R AB, C AB, R BA and C BA - 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<=10 3.  For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.  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 10 4. 

    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种货币,货币之间按照汇率交换,当然还要花费一些手续费,货币交换是可以多次重复进行的,问有没有可能经过一系列的货币交换,开始的货币会增加?
    当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。

    解题思路:这道题可以抽象为图论中的题,将货币种类看为点,货币之间的交换看为有向边,想要货币的金额产生增加,那么必然要有正权回路,即在一条回路上能够一直松弛下去。该题的问题主要在于所给的参数很多,第一行给出了n种货币有m种交换方式,给你第s种货币有V的金额,对于m种的交换方式,从x到y需要汇率rate和手续费commission,从y到x也需要这两个参数。同时这里的松弛递推公式也要发生变化:
                if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
                {
                    dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
                }
    因为是需要增加的正权回路,所以如果小于就松弛。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define inf 0x3f3f3f3f
    struct Edge
    {
        int f;
        int t;
        double r;
        double c;
    } edge[1010];
    double dist[605];
    int n,m,s,cnt;
    double x;
    int bellman_ford()
    {
        int i,j;
        int flag;
        for(i=1; i<=n; i++)
        {
            dist[i]=0;
        }
        dist[s]=x;
        for(j=1; j<=n; j++)
        {
            flag=0;
            for(i=1; i<=cnt; i++)
            {
                if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
                {
                    dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
                    flag=1;
                }
            }
            if(flag==0)
            {
                break;
            }
        }
        return flag;
    }
    int main()
    {
        int i,t;
        int u,v;
        double a1,a2,b1,b2;
        while(scanf("%d%d%d%lf",&n,&m,&s,&x)!=EOF)
        {
            cnt=1;
            while(m--)
            {
                scanf("%d%d%lf%lf%lf%lf",&u,&v,&a1,&b1,&a2,&b2);
                edge[cnt].f=u;
                edge[cnt].t=v;
                edge[cnt].r=a1;
                edge[cnt++].c=b1;
                edge[cnt].f=v;
                edge[cnt].t=u;
                edge[cnt].r=a2;
                edge[cnt++].c=b2;
            }
            if(bellman_ford())
            {
                printf("YES
    ");
            }
            else
            {
                printf("NO
    ");
            }
        }
        return 0;
    }
    
    

     附上使用SPFA的代码

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int maxs = 1e3+200;
    int n,m;
    struct Edge
    {
        int to;
        double rate;
        double com;
    } ;
    double dis[maxs];
    int vis[maxs];
    int cnt[maxs];///用来记录入队列次数
    vector<Edge>maps[maxs];
    void AddEdge(int u,int v,double r,double co)
    {
        Edge t;
        t.to=v;
        t.rate=r;
        t.com=co;
        maps[u].push_back(t);
    }
    int SPFA(int s, double v)
    {
        int i;
        memset(dis,0,sizeof(0));
        memset(vis,0,sizeof(0));
        memset(cnt,0,sizeof(0));
        queue<int>q;
        dis[s]=v;
        vis[s]=1;
        cnt[s]++;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            vis[u]=0;
            for(i=0; i<maps[u].size(); i++)
            {
                int to=maps[u][i].to;
                double com=maps[u][i].com;
                double rate=maps[u][i].rate;
                if(dis[to]<(dis[u]-com)*rate)
                {
                    dis[to]=(dis[u]-com)*rate;
                    if(!vis[to])
                    {
                        vis[to]=1;
                        cnt[to]++;
                        if(cnt[to]>=n)
                        {
                            return 1;
                        }
                        q.push(to);
                    }
                }
            }
        }
        return 0;
    }
    int main()
    {
        int s,i;
        double k;
        while(scanf("%d%d%d%lf",&n,&m,&s,&k)!=EOF)
        {
            int a,b;
            double c,d,e,f;
            while(m--)
            {
                scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
                AddEdge(a,b,c,d);
                AddEdge(b,a,e,f);
            }
            if(SPFA(s,k))
            {
                puts("YES");
            }
            else
            {
               puts("NO");
            }
        }
        return 0;
    }
    
    
    
    
    
  • 相关阅读:
    生成随机串码并保存到Excel中
    制作100份word表
    抓取网页图片-以本地IIS网页为实践对象
    使用xlsxwriter 创建图表chart
    照片查看器2.0
    编程注意事项-记踩过的坑
    STC12C5A60S2的定时器模式16位的时候没有自动重载功能
    Keil C51 一个警告 '=': pointer: different mspace
    RT-Thread 使用笔记二
    Keil-C51读取ROM数据
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/10535437.html
Copyright © 2011-2022 走看看