题目链接:http://poj.org/problem?id=1860
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 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 ( - 0.39) * 29.75 = .3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from to N to each currency. Then each exchange point can be described with 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 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. <=S<=N<=, <=M<=, V is real number, <=V<=.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, -<=rate<=, <=commission<=.
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 .
Output If Nick can increase his wealth, output YES, in other case output NO to the output file.
Sample Input 20.0
1.00 1.00 1.00 1.00
1.10 1.00 1.10 1.00
Sample Output YES
题意:有N种货币 每两种货币之间有汇率和佣金 问是否能通过转换钱币使原来的钱数增加
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 1010
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
struct node
{
int v,next;
double vl,va;
}Map[N];
int s[N],k,vis[N],num[N];
double dis[N],vl;
int n,m,p;
void add(int u,int v,double vl,double va)
{
Map[k].v=v;
Map[k].next=s[u];
Map[k].vl=vl;
Map[k].va=va;
s[u]=k++;
}
int spfa()
{
met(vis,);met(num,);met(dis,);
queue<int>Q;
Q.push(p);
vis[p]=;
dis[p]=vl;
num[p]++;
while(Q.size())
{
int u=Q.front();
Q.pop();
vis[u]=;
for(int i=s[u];i!=-;i=Map[i].next)
{
int v=Map[i].v;
if(dis[v]<(dis[u]-Map[i].va)*Map[i].vl)
{
dis[v]=(dis[u]-Map[i].va)*Map[i].vl;
if(!vis[v])
{
Q.push(v);
vis[v]=;
num[v]++;
if(num[u]>n)
return ;
}
} } }
if(dis[p]>vl)
return ;
return ;
}
int main()
{
int a,b;
double v1,v2,v3,v4;
while(scanf("%d %d %d %lf",&n,&m,&p,&vl)!=EOF)
{
met(s,-);
k=;
for(int i=;i<m;i++)
{
scanf("%d %d %lf %lf %lf %lf",&a,&b,&v1,&v2,&v3,&v4);
add(a,b,v1,v2);
add(b,a,v3,v4);
}
if(spfa())
printf("YES\n");
else
printf("NO\n");
}
return ;
}