poj1860 货币转换 spfa+Bellman-Ford

<div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Currency Exchange</div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 30000K</td></tr><tr><td><strong>Total Submissions:</strong> 24042</td><td width="10px"> </td><td><strong>Accepted:</strong> 8729</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">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<sub>AB</sub>, C<sub>AB</sub>, R<sub>BA</sub> and C<sub>BA</sub> - 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. 
</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">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<sup>3</sup>. 
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10<sup>-2</sup><=rate<=10<sup>2</sup>, 0<=commission<=10<sup>2</sup>. 
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<sup>4</sup>. 
</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">If Nick can increase his wealth, output YES, in other case output NO to the output file.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace;font-size:14px;">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

spfa:

#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
int n,m,s;
double v;
double  rat[1000][1000];
double cost[1000][1000];
double dis[1000];
int spfa(int start)
{
    queue<int >q;
    int  visit[110];
    memset(visit,0,sizeof(visit));
    memset(dis,0,sizeof(dis));
    dis[start]=v;
    q.push(start);
    visit[start]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        visit[x]=0;
        for(int i=0; i<=n; i++)
        {
            if(dis[i]<(dis[x]-cost[x][i])*rat[x][i])
            {
                dis[i]=(dis[x]-cost[x][i])*rat[x][i];
                if(dis[start]>v)
                {
                    return 1;
                }
                if(!visit[i])
                {
                    q.push(i);
                    visit[i]=1;

                }
            }
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(i==j)
                    rat[i][j]=1;
                else  rat[i][j]=0;
                cost[i][j]=0;
            }
        }
        int a,b;
        double rab,cab,rba,cba;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
            rat[a][b]=rab;
            cost[a][b]=cab;
            rat[b][a]=rba;
            cost[b][a]=cba;
        }
        if(spfa(s))
        {
            printf("YES\n");
        }
        else printf("NO\n");
    }
}

Bellman-Ford

#include<stdio.h>
#include<string.h>
typedef struct
{
    int a,b;
   double  rate;
    double cost;
} node;
node q[1000];
int n,m,s;
double v;
int count;
double dis[1000];
int suan()
{
    memset(dis,0,sizeof(dis));
    dis[s]=v;
    for(int i=1; i<n; i++)
    {
        int flag=0;
        for(int j=0; j<count; j++)
        {
            int a=q[j].a;
            int b=q[j].b;
            double  r=q[j].rate;
           double  c=q[j].cost;
            if(dis[b]<(dis[a]-c)*r)
            {
                dis[b]=(dis[a]-c)*r;
                flag=1;
            }
        }
        if(flag==0)
            break;
    }
    for(int  i=0; i<count; i++)

        if(dis[q[i].b]<(dis[q[i].a]-q[i].cost)*q[i].rate)
            return 1;
        return 0;

}
int main()
{
    while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
    {
        count=0;
        int a,b;
        double rab,rba,cab,cba;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
            q[count].a=a;
            q[count].b=b;
            q[count].rate=rab;
            q[count++].cost=cab;
            q[count].a=b;

            q[count].b=a;
            q[count].rate=rba;
            q[count++].cost=cba;
        }
        if(suan())
        {
            printf("YES\n");

        }
        else printf("NO\n");
    }
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/FengTwoYear/article/details/50319461
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞