[POJ 1860] Currency Exchange [spfa]

10084 단어 poj

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 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;
}

좋은 웹페이지 즐겨찾기