题目描述:
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 R AB, C AB, R BA and C BA - 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.
输入:
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<=10 3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.
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 10 4.
输出:
If Nick can increase his wealth, output YES, in other case output NO to the output file.
样例输入:
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
样例输出:
YES
题目大意:
给n种钱,m个换钱点,每个换钱点可以把你手中的前扣掉手续费之后乘以汇率换成另一种,钱b =(钱a - 手续费c)*汇率r 。问给你开始给你v块种类为s的钱,有没有可能通过换钱还钱之后把钱变多。
思路:
很是标准的一个判断负环的问题,可以用spfa算法,也可以bellman-ford,当然在数据量比较小的时候,还可以floyed,当然本题不行
code:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int n,m,s;
double v;
struct node{
int from,to;
double cost,rate;
}feng[305];
double d[305];
bool find_negative_loop()
{
memset(d,0,sizeof(d));
d[s]=v;
for(int i=0;i<n;i++)
{
for(int j=0;j<2*m;j++)
{
node jiang=feng[j];
double much=(d[jiang.from]-jiang.cost)*jiang.rate;
if((much-d[jiang.to])>1e-6)
{
d[jiang.to]=much;
if(i==n-1)return true;
}
}
}
return false;
}
int main()
{
while(~scanf("%d%d%d%lf",&n,&m,&s,&v))
{
for(int i=0;i<2*m;i+=2)
{
int a,b;
double rab,rba,cab,cba;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
feng[i].from=a;
feng[i].to=b;
feng[i].rate=rab;
feng[i].cost=cab;
feng[i+1].from=b;
feng[i+1].to=a;
feng[i+1].rate=rba;
feng[i+1].cost=cba;
}
if(find_negative_loop())printf("YES\n");
else printf("NO\n");
}
return 0;
}