码迷,mamicode.com
首页 > 其他好文 > 详细

[kuangbin带你飞]专题四 最短路练习 E - Currency Exchange

时间:2019-07-19 21:17:11      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:HERE   stream   names   sequence   链接   ted   nta   some   nts   

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\n");
    else
        printf("NO\n");
    return 0;
}

 



[kuangbin带你飞]专题四 最短路练习 E - Currency Exchange

标签:HERE   stream   names   sequence   链接   ted   nta   some   nts   

原文地址:https://www.cnblogs.com/Vampire6/p/11215515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!