美文网首页
poj1860- Currency Exchange

poj1860- Currency Exchange

作者: httpsbao | 来源:发表于2019-03-23 20:22 被阅读0次

题目传送:poj1860
题目描述:

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

题目大意:有M种货币兑换方式,可以兑换N种货币,每种兑换方式只能互换两种货币,且汇率与手续费各不相同。某人初始时手中有S货币V元,问他是否有可能通过货币兑换,在最后换回货币S时实现盈利,有可能则输出YES,否则输出NO.
题目分析:把每种货币做为节点编号,边的权值为汇率和手续费,判断图中是否存在正权回路。若有,说明可以盈利;若没有,说明不能盈利。与最短路不同的是松弛条件.

spfa

#include<cstdio>
#include<cstring>//memset函数头文件
#include<queue>
using namespace std;
const int maxn=110;
int n,m,s;
double dis[maxn],v,rate[maxn][maxn],cost[maxn][maxn];//rate[]汇率,cost[]手续费
bool spfa(int start){
    bool book[110];
    memset(book,0,sizeof(book));
    memset(dis,0,sizeof(dis));
    dis[start]=v;
    queue<int>q;
    q.push(start);
    book[start]=true;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        book[x]=false;
        for(int i=1;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(!book[i]){
                    q.push(i);
                    book[i]=true;
                }
            }
        }
    }
    return false;
}
int main(){
    while(~scanf("%d%d%d%lf",&n,&m,&s,&v)){
        int a,b;
        double rab,rba,cab,cba;
        //cost数组,rate数组初始化
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cost[i][j]=0;
                if(i==j)
                    rate[i][j]=1;
                else
                    rate[i][j]=0;
            }
        }
        //读入两种货币编号以及他们之间的汇率和手续费
        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;
}

相关文章

网友评论

      本文标题:poj1860- Currency Exchange

      本文链接:https://www.haomeiwen.com/subject/mhosvqtx.html