• [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;
        
    }
  • 相关阅读:
    实现 HTML页面 Table 标签分页打印
    windows定时休眠设置
    python画树
    anaconda历史版本
    枚举
    is 与 as 运算符举例
    Microsoft.ACE.OLEDB.12.0报错解决方法
    winform一个带自动完成功能的TextBox
    DWZ中整合第三方jQuery(kit日历控件)插件
    asp.net 使用NPOI,泛型反射,XML导入导出excel
  • 原文地址:https://www.cnblogs.com/evenbao/p/9689782.html
Copyright © 2020-2023  润新知