Currency Exchange POJ - 1860 spfa判断正环

//spfa 判断正环
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=1e4;
const int INF=2e9;
int h[N],to[N],ne[N],idx;
double r[N],c[N];
int n, m,X;
double V;
void add(int u,int v,double r1,double c1)
{
    to[idx]=v;
    r[idx]=r1;
    c[idx]=c1;
    ne[idx]=h[u];
    h[u]=idx++;
}
double dist[N];
int times[N];
bool vis[N];
bool spfa(int st)
{
    memset(vis,0,sizeof vis);
    memset(times,0,sizeof times);
    for(int i=1;i<=n;i++)
        dist[i]=-INF;
    queue<int>q;
    q.push(st);
    vis[st]=1;
    dist[st]=V;
    while(q.size())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=h[u];~i;i=ne[i])
        {
            int v=to[i];
            if(dist[v]<(dist[u]-c[i])*r[i])
            {
                dist[v]=(dist[u]-c[i])*r[i];
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                    if(++times[v]>n)
                        return true;
                }
            }
        }
    }
    return false;
}
int main()
{
    while(cin>>n>>m>>X>>V)
    {
        idx=0;
        memset(h,-1,sizeof h);
        for(int i=1;i<=m;i++)
        {
            int u,v;
            double r1,c1,r2,c2;
            cin>>u>>v>>r1>>c1>>r2>>c2;
            add(u,v,r1,c1);
            add(v,u,r2,c2);
        }
        if(spfa(X))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12235624.html

时间: 2024-11-08 14:34:19

Currency Exchange POJ - 1860 spfa判断正环的相关文章

POJ 1860 (SPFA判断正环)

题意:我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑换.每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A.同时各交换点有一些佣金,你要为你的交换操作的总和.在来源货币中总是收取佣金. 例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布. 你肯定知道在我们的城市里你可以处理不同的货币.让每一种货币都用唯一的一个小于N的整数表示.然

HDU 1317(Floyd判断连通性+spfa判断正环)

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4421    Accepted Submission(s): 1252 Problem Description It has recently been discovered how to run open-source software on the Y-Crate gami

hdu1317(spfa判断正环+Floyd判断联通)

题意很好理解,判断是否能从起点走到终点,每走到几个点就会获得这个点所代表的能量值,起点时自身带有100能量值. 刚开始写了个裸地spfa,超时了,发现可能存在从起点走不到终点,而且还存在正环,这样程序永远也不会结束,改正后用Floyd判断起点和终点是否联通,然后用spfa求最长路,遇到环中的点时判断,环中的点是否能到达终点,能到达则输出“winnable”,因为可以在正环中获得无限能量值. 不能的话继续做spfa, 并且环中的点不再进入队列,这个换就被破坏了,这样就算图中有多个环也能处理.若没有

HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5304    Accepted Submission(s): 1510 Problem Description It has recently been discovered how to run open-source software on the Y-Crate gam

Wormholes POJ - 3259 spfa判断负环

//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int N=1e5,INF=0x3f3f3f3f; int dist[N]; int h[N],e[N],ne[N],w[N],idx; int n,m,z; void add(int a,int b,in

Currency Exchange - poj 1860

Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22111   Accepted: 7986 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange op

POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11526   Accepted: 3930 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big ci

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 货币兑换 SPFA判正环

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20280   Accepted: 7270 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe