poj 1860 Currency Exchange(SPFA)

题目链接:http://poj.org/problem?id=1860

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

Source

Northeastern Europe 2001, Northern Subregion

Description
一些货币兑换点正在我们的城市工作。我们假设每个兑换点尤其擅长兑换两种特定的货币,并且只进行对这些货币的兑换。可能有一些兑换点专门兑换相同的货币对。每个兑换点都有自己的汇率,货币A到货币B的汇率是你用1货币A换到的货币B的数量。每个兑换点也有一些佣金,即为你需要为你的兑换行动支付的金额。佣金总是从源货币扣除。
例如,如果你想要在汇率为29.75,并且佣金为0.39的兑换点将100美元兑换成俄罗斯卢布,你将会得到(100-0.39)*29.75=2963.3975卢布。
你有N种不同的货币可以在我们的城市进行兑换。我们为每一种货币制定从1到N的唯一一个整数。每个兑换点可以用6个数字形容:整数A和B——它交换的货币(表示为货币A和货币B);实数RAB,CAB,RBA和CBA——当它分别将货币A兑换成货币B和将货币B兑换成货币A时的汇率和佣金。
叫兽有一定数量的货币S,并且它希望它能以某种方式在一些兑换行动后增加它的资本。最后它的资金必须兑换为货币S。
帮助它解决这个棘手的问题。叫兽在进行它的兑换行动时资金总数必须始终非负。
Input
输入的第一行包含四个数字:N – 货币的数量,M – 兑换点的数量,S – 叫兽具有的货币种类,V – 叫兽具有的货币数量。
下面的M行每行包括6个数字 – 对相应的兑换点的描述。数字相隔一个或多个空格。1<=S<=N<=100,1<=M<=100,V是实数,0<=V<=10^3。
对于每个兑换点汇率和佣金都是实数,小数点后最多有两位小数。10^-2<=汇率<=10^2,0<=佣金<=10^2。
如果在一组兑换行动中没有兑换点被超过一次地使用,我们认为这组兑换行动是简单的。你可以认为最终数值和最初任何一个简单的兑换行动组的比例小于10^4。
Output
如果叫兽能增加它的财产,输出YES,否则输出NO。
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

思路:我们只需要寻找一下是否有正环存在就可以了!

代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define pi acos(-1.0)
#define INF 0xffffff
#define N 3005
#define M 2005
int Edgehead[N];
int cont[N];
double dis[N];
struct
{
    int v,next;
    double rate, comm, w;
}Edge[2*M];
bool vis[N];
int k;
int n, m, s;
double val;
void Addedge(int u,int v,double r, double c)
{
    Edge[k].next = Edgehead[u];
    Edge[k].rate = r;
    Edge[k].v = v;
    Edge[k].comm = c;
    Edge[k].w = 0;
    Edgehead[u] = k++;
}
void init()
{
    for(int i = 0 ; i < n ; i++ )//求最长路径开始设为0
        dis[i] = 0;
    memset(Edgehead,-1,sizeof(Edgehead));
    memset(vis,false,sizeof(vis));
    memset(cont,0,sizeof(cont));
}
int SPFA( int start)
{
    queue<int>Q;
    dis[start] = val;
    vis[start] = true;
    ++cont[start];
    Q.push(start);
    while(!Q.empty())//直到队列为空
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = Edgehead[u] ; i!=-1 ; i = Edge[i].next)//注意
        {
            int v = Edge[i].v;
            Edge[i].w = (dis[u] - Edge[i].comm)*Edge[i].rate-dis[u];
            if(dis[v] < dis[u] + Edge[i].w)
            {
                dis[v] = dis[u]+Edge[i].w;
                if( !vis[v] )//防止出现环,也就是进队列重复了
                {
                    Q.push(v);
                    vis[v] = true;
                }
                if(++cont[v] > n)//有负环
                    return -1;
            }
        }
    }
    return 1;
}
int main()
{
    while(~scanf("%d%d%d%lf",&n,&m,&s,&val))//n为目的地
    {
        k = 0;
        init();
        for(int i = 1 ; i <= m ; i++ )
        {
            int a, b;
            double r, c;
            scanf("%d%d",&a,&b);
            scanf("%lf%lf",&r,&c);
            Addedge(a, b, r, c);//双向链表
            scanf("%lf%lf",&r,&c);
            Addedge(b, a, r, c);//双向链表
        }
        if(SPFA(s) == -1)
        {
            printf("YES\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}  

poj 1860 Currency Exchange(SPFA)

时间: 2024-12-18 12:55:15

poj 1860 Currency Exchange(SPFA)的相关文章

poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

链接:poj 1860 题意:给定n中货币,以及它们之间的税率,A货币转化为B货币的公式为 B=(V-Cab)*Rab,其中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会增加 分析:这个题就是判断是否存在正权回路,可以用bellman-ford算法,不过松弛条件相反 也可以用SPFA算法,判断经过转换后,转换为原本货币的值是否比原值大... bellman-ford    0MS #include<stdio.h> #include<string.h> str

POJ - 1860 Currency Exchange(SPFA或Floyd)

题目链接:http://poj.org/problem?id=1860 题意:货币之间转换问题,有N种钱,M种转换,初始第S种钱有V价值,然后转换(转换公式:(钱价值-税)*汇率),问是否能有一种转换可以是初始S的钱价值增加. 题解: 1.SPFA 因为可能钱会减少,就是可能出现负边,所以肯定不能用dijkstra. SPFA的话就是把每种情况都搜寻一下,判断初始的钱是否增加. 2.Floyd 看到网上有人用Floyd做,用两次Floyd,找正环,就是第一次相当于赋予一个初始值,然后第二次,如果

poj 1860 Currency Exchange(Bellman-Ford 改)

poj 1860 Currency Exchange 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

POJ 1860 Currency Exchange (最短路)

Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 4   Accepted Submission(s) : 2 Problem Description Several currency exchange points are working in our city. Let us suppose that

POJ 1860 Currency Exchange(BellmanFord算法的变形,求正环)

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 o

POJ 1860 Currency Exchange(如何Bellman-Ford算法判断正环)

题目链接: https://cn.vjudge.net/problem/POJ-1860 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 s

(简单) POJ 1860 Currency Exchange,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

POJ 1860: Currency Exchange 【SPFA】

套汇问题,从源点做SPFA,如果有一个点入队次数大于v次(v表示点的个数)则图中存在负权回路,能够套汇,如果不存在负权回路,则判断下源点到自身的最长路是否大于自身,使用SPFA时松弛操作需要做调整 #include<iostream> #include<cstdio> #include<string.h> #include <stdlib.h> #include <math.h> using namespace std; const int ma

Dijkstra算法(求解单源最短路)详解 + 变形 之 poj 1860 Currency Exchange

/* 求解单源最短路问题:Dijkstra算法(该图所有边的权值非负) 关键(贪心): (1)找到最短距离已经确定的节点,从它出发更新与其相邻节点的最短距离: (2)此后不再关心(1)中“最短距离已经确定的节点”. 时间复杂度(大概的分析,不准确): “找到最短距离已经确定的节点” => O(|V|) "从它出发更新与其相邻节点的最短距离" => 邻接矩阵:O(|V|),邻接表:O(|E|) 需要循环以上两个步骤V次,所以时间复杂度:O(V^2) 即:在|E|较小的情况下,