poj 1860 Currency Exchange (spfa求最长路)

poj 1860 题目链接

题意:有n种货币,m个货币交易点,每个货币交易点只能是两种货币之间交换,给出两个方向的汇率和手续费。初始拥有数量v的货币s,问能否经过一些py交易,使得最后手里的货币s比v多。

思路:大概还是用spfa求最长路。。松弛那里需要注意一下算法。。。

1A。。。好爽啊。。。。。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年05月24日 星期二 23时41分46秒
  4File Name :code/poj/1860.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=1E2+7;
 34int n,m,s;
 35double v;
 36double d[N];
 37bool inq[N];
 38vector <pair<int,pair<double,double> > > edge[N];
 39
 40int dblcmp(double d)
 41{
 42    return d<-eps?-1:d>eps;
 43}
 44bool spfa(int s,double amt)
 45{
 46    ms(d,0);
 47    ms(inq,false);
 48    queue<int>q;
 49    q.push(s);
 50    inq[s] = true;
 51    d[s] = amt;
 52
 53    while (!q.empty())
 54    {
 55	int u = q.front();
 56	q.pop();
 57	inq[u] = false;
 58
 59	int siz = edge[u].size();
 60	for  ( int i = 0 ; i  < siz; i ++)
 61	{
 62	    int v = edge[u][i].fst;
 63	    double r = edge[u][i].sec.fst;
 64	    double c = edge[u][i].sec.sec;
 65	    double tmp = (d[u]-c)*r;
 66	    if (dblcmp(d[v]-tmp)<0)
 67	    {
 68		d[v] = tmp;
 69		if (inq[v]) continue;
 70		inq[v] = true;
 71		q.push(v);
 72	    }
 73	}
 74    }
 75    return d[s]>amt;
 76}
 77int main()
 78{
 79	#ifndef  ONLINE_JUDGE 
 80	freopen("code/in.txt","r",stdin);
 81  #endif
 82
 83	ios::sync_with_stdio(false);
 84	cin>>n>>m>>s>>v;
 85	for ( int i = 1 ; i <= m ; i++)
 86	{
 87	    int u,v;
 88	    double r1,c1,r2,c2;
 89	    cin>>u>>v>>r1>>c1>>r2>>c2;
 90	    edge[u].push_back(MP(v,MP(r1,c1)));
 91	    edge[v].push_back(MP(u,MP(r2,c2)));
 92	}
 93
 94	if (spfa(s,v)) 
 95	{
 96	    puts("YES");
 97	}
 98	else
 99	{
100	    puts("NO");
101	}
102  #ifndef ONLINE_JUDGE  
103  fclose(stdin);
104  #endif
105    return 0;
106}