zoukankan      html  css  js  c++  java
  • [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;
        
    }
  • 相关阅读:
    云计算安全概述
    快照技术
    存储可靠性技术之--备份
    存储可靠性技术之 --RAID
    存储方式
    存储技术
    CentOS安装setup
    CentOS7安装iptables防火墙
    CentOS 7.0下使用yum安装MySQL
    The APR based Apache Tomcat Native library
  • 原文地址:https://www.cnblogs.com/evenbao/p/9689782.html
Copyright © 2011-2022 走看看