[POJ 1860] Currency Exchange

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 1860] Currency Exchange相关的知识,希望对你有一定的参考价值。

[题目链接]

        http://poj.org/problem?id=1860

[算法]

       SPFA判负环

       时间复杂度 : O(kn)

[代码]

       

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 110
const int inf = 2e9;

int n , m , s , tot;
int head[MAXN];
double v;

struct edge
{
        int to;
        double va,vb;
        int nxt;
} e[MAXN << 1];

inline void addedge(int u,int v,double a,double b)
{
        tot++;
        e[tot] = (edge){v,a,b,head[u]};
        head[u] = tot;        
}
inline bool spfa(int s)
{
        queue< int > q;
        static int cnt[MAXN];
        static bool inq[MAXN];
        static double dist[MAXN];
        for (int i = 1; i <= n; i++) 
        {
                dist[i] = -inf;        
                cnt[i] = 0;
                inq[i] = false;
        }
        inq[s] = true;
        cnt[s] = 1;
        dist[s] = v;
        q.push(s);
        while (!q.empty())
        {
                int cur = q.front();
                inq[cur] = false;
                q.pop();
                for (int i = head[cur]; i; i = e[i].nxt)
                {
                        int to = e[i].to;
                        pair<double,double> value = make_pair(e[i].va,e[i].vb);
                        if ((dist[cur] - value.second) * value.first > dist[to])
                        {
                                dist[to] = (dist[cur] - value.second) * value.first;
                                inq[to] = true;
                                if (++cnt[to] > n) return true;
                                q.push(to);
                        }
                }
        }
        return false;
}

int main()
{
        
        while (scanf("%d%d%d%lf",&n,&m,&s,&v) != EOF)
        {
                tot = 0;
                for (int i = 1; i <= n; i++) head[i] = 0;    
                for (int i = 1; i <= m; i++)
                {
                        int u , v;
                        double a1 , b1 , a2 , b2;
                        scanf("%d%d%lf%lf%lf%lf",&u,&v,&a1,&b1,&a2,&b2);
                        addedge(u,v,a1,b1);
                        addedge(v,u,a2,b2);
                }            
                if (spfa(s)) printf("YES
");
                else printf("NO
");
        }
        
        return 0;
    
}

 

以上是关于[POJ 1860] Currency Exchange的主要内容,如果未能解决你的问题,请参考以下文章

POJ-1860 Currency Exchange spfa判负环

POJ——T1860 Currency Exchange

POJ 1860 Currency Exchange

POJ 1860 Currency Exchange (最短路)

poj1860 Currency Exchange(spfa判断正环)

POJ1860 Currency Exchange