zoukankan      html  css  js  c++  java
  • [kuangbin带你飞]专题四 最短路练习 E

    E - Currency Exchang

    题目链接:https://vjudge.net/contest/66569#problem/E

    题目:

    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种货币可以相互转换,之间有汇率和手续费,问是否经过转换点的转换使得货币增多
    思路:判断图是否存在负权环,用spfa算法可以高效判断是否存在负权环,只要发现一个点入队列次数大于n,则就存在负权环使得货币增加,因为一个点的入队次数一定小于等于(n-1)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int maxn=1005;
    int book[maxn],head[maxn],cnt[maxn];
    int pos,n,m,s;
    double d[maxn],v;
    struct Edge{
        int to,next;
        double r,c;
    }edge[maxn];
    void add(int a,int b,double r,double c)
    {
        edge[pos].to=b;
        edge[pos].r=r;
        edge[pos].c=c;
        edge[pos].next=head[a];
        head[a]=pos++;
    }
    bool spfa()
    {
        memset(book,0,sizeof(book));
        memset(d,0,sizeof(d));
        memset(cnt,0,sizeof(cnt));
        queue<int>qu;
        qu.push(s);
        book[s]=1;
        d[s]=v;
        cnt[s]++;
        while(!qu.empty())
        {
            int now=qu.front();
            qu.pop();
            book[now]=0;
            for(int i=head[now];i!=-1;i=edge[i].next)
            {
                int toto=edge[i].to;
                double rr=edge[i].r;
                double cc=edge[i].c;
                if(d[toto]<(d[now]-cc)*rr)
                {
                    d[toto]=(d[now]-cc)*rr;
                    if(!book[toto])
                    {
                        book[toto]=1;
                        cnt[toto]++;
                        qu.push(toto);
                    }
                    if(cnt[toto]>n)
                        return true;
                }
            }
        }
        return false;
    }
    int main()
    {
        scanf("%d%d%d%lf",&n,&m,&s,&v);
        pos=0;
        memset(head,-1,sizeof(head));
        int a,b;
        double r,c;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%lf%lf",&a,&b,&r,&c);
            add(a,b,r,c);
            scanf("%lf%lf",&r,&c);
            add(b,a,r,c);
        }
        if(spfa())
            printf("YES
    ");
        else
            printf("NO
    ");
        return 0;
    }


  • 相关阅读:
    python 爬取豆瓣电影短评并wordcloud生成词云图
    基于javaweb人脸识别注册登录系统
    html/jsp导出pdf格式的几种方法(jsPDF,iText,wkhtmltopdf)
    微信小程序wx.getLocation()获取经纬度及JavaScript SDK调用腾讯地图API获取某一类地址
    微信小程序登录流程及解析用户openid session_key,获取用户信息
    Windows Server 2012 R2服务器部署Tomcat JDK、安装Mysql以及将Java项目部署到CVM
    Tomcat出现端口占用错误
    linux cmake error undefined reference to symbol 'pthread_create@@GLIBC_2.2.5
    CMake Warning This command specifies the relative path as a link directory.
    linux vim查看文件编码格式
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11215515.html
Copyright © 2011-2022 走看看