1. 程式人生 > >[POJ 1860] Currency Exchange

[POJ 1860] Currency Exchange

first har truct %d while fir div pan tde

[題目鏈接]

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\n"); else printf("NO\n"); } return 0; }

[POJ 1860] Currency Exchange