【POJ 1860】Currency Exchange

本文介绍了一种利用SPFA算法判断是否存在通过货币兑换获得无限增益的方法。具体实现包括如何构建图结构、进行最短路径计算及检测负权环。若存在一条能使初始货币无限增值的路径,则算法返回“YES”,否则返回“NO”。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

【题意】

给你n种货币,m种货币之间的交换信息;
交换信息以
A,B,RA,CA,RB,CB的形式给出;
即A换B的话假设A有x元则换成B就变成(X-CA)*RA
B换成A的话同理;
然后你一开始只有某一种x货币;
问你可不可能通过换货币来获得更多x货币;
即最后又要换回来.

【题解】

可以在做SPFA的最长路的时候判断能不能出现环.
具体的。
只要从x出去了,然后又回来了;
就说明能够在经过某些路径之后这个x又变大了;
任意哪一个x都可以;
因为你可以通过这条路径让x变得无穷大;
那么起点s肯定也能无穷大啦;
判断环的话只要判断一个点是否入队n次就好;

【Number Of WA

1

【完整代码】

#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

struct abc
{
    int en;
    double r,c;
};

int n,m,s,num[N];
bool inq[N];
double dis[N];
vector <abc> G[N];
queue <int> dl;

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    memset(dis,-INF,sizeof dis);
    cin >> n >> m >> s;cin >> dis[s];
    for (int i = 1;i <= m;i++)
    {
        int x,y;
        double r,c;
        abc temp;
        cin >>x >> y >> r >> c;
        temp.r = r,temp.c = c,temp.en = y;
        G[x].push_back(temp);
        cin >> r >> c;
        temp.r = r,temp.c = c,temp.en = x;
        G[y].push_back(temp);
    }
    dl.push(s);
    num[s]=1;
    inq[s] = true;
    while (!dl.empty())
    {
        int x = dl.front();
        inq[x] = false;
        dl.pop();
        int len = G[x].size();
        for (int i = 0;i <= len-1;i++)
        {
            abc temp1 = G[x][i];
            int y = temp1.en;
            double ju = (dis[x]-temp1.c)*temp1.r;
            if (ju>0)
            {
                if (dis[y]<ju)
                {
                    dis[y] = ju;
                    if (!inq[y])
                    {
                        num[y]++;
                        if (num[y]>n)
                            return cout <<"YES"<<endl,0;
                        dl.push(y);
                        inq[y]=true;
                    }
                }
            }
        }
    }
    cout <<"NO"<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626405.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值