最短路练习5/poj/1860 /Currency Exchange 有正环

来源:互联网 发布:java实战项目百度云 编辑:程序博客网 时间:2024/06/08 18:12


题目链接:http://poj.org/problem?id=1860
Currency Exchange
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 29488 Accepted: 11052

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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00

Sample Output

YES
题意:给出了货币交换的汇率和佣金,求是否能通过货币转换而使钱变多;

思路:图里面一定有正环才能使得钱变多,如果找不到正环,则一定输出NO,找到就输出YES;

Bellman_Ford算法:AC代码:

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<map>#include<string>#define LL long long#define eps 1e-8using namespace std;const int mod = 1e7+7;const int inf = 0x3f3f3f3f;const int maxn = 1e6 +10;int n,m,s,a,b,point;double c,d,e,f,v;double ma[105][105];//汇率double mb[105][105];//佣金double dis[105];//可以转换成i型的钱的最大值int main(){    while(~scanf("%d%d%d%lf",&n,&m,&s,&v))    {        memset(ma,0,sizeof(ma));        memset(mb,0,sizeof(mb));        memset(dis,0,sizeof(dis));        for(int i=1;i<=n;i++)        {            ma[i][i]=1.0;            mb[i][i]=0;        }        for(int i=0;i<m;i++)        {           scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);            ma[a][b]=c;            ma[b][a]=e;            mb[a][b]=d;            mb[b][a]=f;        }            dis[s]=v;            while(dis[s]<v+eps)//如果已经dis[s]已经增大直接跳出;            {                int flag=0;                for(int i=1;i<=n;i++)                    for(int j=1;j<=n;j++)                        if(dis[j]+eps<(dis[i]-mb[i][j])*ma[i][j])//能找到可以增大的值;                        {                            dis[j]=(dis[i]-mb[i][j])*ma[i][j];                            flag=1;                        }                if(!flag)//找不到能增大的值就跳出。                    break;            }          if(dis[s]>v+eps)            printf("YES\n");        else            printf("NO\n");    }}

spfa算法:AC代码:

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<map>#include<string>#define LL long long#define eps 1e-8using namespace std;const int mod = 1e7+7;const int inf = 0x3f3f3f3f;const int maxn = 1e6 +10;int n,m,s,a,b,point;double c,d,e,f,v;double ma[105][105];double mb[105][105];double dis[105];int vis[105];queue<int>q;int main(){    while(~scanf("%d%d%d%lf",&n,&m,&s,&v))    {        while(!q.empty())            q.pop();        memset(ma,0,sizeof(ma));        memset(mb,0,sizeof(mb));        memset(vis,0,sizeof(vis));        memset(dis,0,sizeof(dis));        for(int i=1; i<=n; i++)            ma[i][i]=1.0;        for(int i=0; i<m; i++)        {            scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);            ma[a][b]=c;            ma[b][a]=e;            mb[a][b]=d;            mb[b][a]=f;        }        q.push(s);        dis[s]=v;        vis[s]=1;        int ans=0;        while(!q.empty())        {            int p=q.front();            q.pop();            vis[p]=0;            for(int i=1; i<=n; i++)            {                if(dis[i]<(dis[p]-mb[p][i])*ma[p][i])//如果一直不能更新值,当队列为空时就是找不到,输出NO                {                    dis[i]=(dis[p]-mb[p][i])*ma[p][i];                    if(dis[s]>v)//如果找到跳出输出YES                     {                         ans=1;                         break;                    }                      if(!vis[i])                      {                          q.push(i);                          vis[i]=1;                      }                }            }            if(ans)                break;        }        if(ans)            printf("YES\n");        else            printf("NO\n");    }}








0 0
原创粉丝点击