[POJ 1860] Currency Exchange [spfa]

Currency Exchange

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 31674 Accepted: 12035

Description

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.

Input

The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.
Sample Input

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output

YES

题意:

城里流通N种货币,有M个货币交易点,每个交易点只有进行两种类型货币a, b互相兑换,a兑换b的汇率为rate1,每笔交易收取的佣金co1, b兑换a汇率为rate2,每笔交易收取佣金co2。
如果你在这个点用F单位a货币兑换b货币,你能得到(F - co1) * rate1单位b货币。
Nick手中有V单位S货币,问Nick能通过交易来让自己的资金增加么?
如果现实也这么简单就好了,简单写段小代码,就坐等资金翻翻翻,岂不美滋滋?

**

题解:

**
题目类似于负圈判断,只是这里改成是否有正权回路罢了。
将题目抽象化, 每种货币作为点, 每个交易点的信息来给两个点(两种货币)建边。注意两种货币的汇率之间并没有必然的关系,所以我们每次用交易点建边时建两条单向边,边上有权值汇率rate和佣金com。
然后套用最短路模型,dis初始化为0,起点dis[s]设为V,每次只要有dis[v]变大就更新。当某一点更新了N次以上,则证明图中存在正权回路。
用spfa和Bellman-Ford都行。

以下是用spfa写的代码。

//#define LOCAL
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

#define LL long long
#define ll long long
#define INF 0x3f3f3f3f
#define maxn MAX_N
#define MOD mod
#define MMT(x,i) memset(x,i,sizeof(x))
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second

const LL MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-8;

const int MAX_V = 1010;
const int MAX_E = 1010;
int N, M, S;
double V;
double dis[MAX_V];
int cnt[MAX_V];
bool vis[MAX_V];
struct edge{
  int to;
  double rate, com;
  edge(int a, double b, double c):to(a), rate(b), com(c){}
};
vector<edge> G[MAX_V];
void addedge(int u, int v, double rate, double com)
{
  G[u].push_back(edge(v, rate, com));
}
bool spfa_bfs(int s)
{
  queue<int> q;
  memset(dis, 0, sizeof dis);             //初值赋为0
  memset(vis, 0, sizeof vis);
  memset(cnt, 0, sizeof cnt);
  dis[s] = V, vis[s] = 1, cnt[s] = 1;
  q.push(s);
  while(!q.empty())
  {
    int x = q.front();
    q.pop();
    vis[x] = 0;
    for(int j = 0; j < G[x].size(); j++)
    {
      int v = G[x][j].to;
      double rate = G[x][j].rate, com  = G[x][j].com;
      if(dis[v] < (dis[x] - com) * rate)        //距离判断及更新
      {
        dis[v] = (dis[x] - com) * rate;       //
        if(!vis[v])
        {
          vis[v] = 1;
          cnt[v]++;
          q.push(v);
          if(cnt[v] > N)  return 0;
        }
      }
    }
  }
  return 1;
}
int main()
{
  #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  #endif
  ios::sync_with_stdio(false);
  cin >> N >> M >> S >> V;
  REP(i, M)
  {
    int u, v;
    double a, b, c, d;
    cin >> u >> v >> a >> b >> c >> d;
    addedge(u, v, a, b);
    addedge(v, u, c, d);
  }
  if(!spfa_bfs(S))  cout << "YES" << endl;
  else  cout << "NO" << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值