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<=1e3.
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 , s , v ,表示有n种货币,m种兑换方法,手上有第 s 种货币 v 元
接下来 m 行,前两个数字 A ,B 表示两种可以相互兑换的货币,之后四个数,前两个表示A兑换B的汇率和佣金,后两个表示B兑换A的汇率和佣金
(当前A - A兑换B的佣金)X A兑换B的汇率 = 可兑换B
具体思路
最大路径基础上把松弛的判断与更新更改一下,只要最后可以兑换回S并且可以使得S在一顿骚操作后增值(外送精美银手镯一副,包吃包住豪华游年 )就可以输出YES了
多组数据记住vector和queue要及时清除,因为这个问题RE和WA了好几发,最后跑完图只要判断dis[s]的值是否大于最初拿出手的v就可以了
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
struct node
{
int to;
double R,C;
};
vector<node>line[105];
double dis[105],v;
bool flag[105];
int n,m,s;
queue<int>dui;
void dijkstra()
{
while(!dui.empty())
{
dui.pop();
}
flag[s] = 1;
dui.push(s);
while(!dui.empty())
{
if(dis[s]>v) return;
int now = dui.front();
dui.pop();
flag[now] = false;
for(int i=0;i<line[now].size();i++)
{
node N=line[now][i];
double tmp=(dis[now]-N.C)*N.R;
if(dis[N.to]<tmp)
{
dis[N.to]=tmp;
if(flag[N.to]==false)
{
flag[N.to]=true;
dui.push(N.to);
}
}
}
}
}
int main()
{
while(scanf("%d %d %d %lf",&n,&m,&s,&v)!=EOF)
{
for (int i=1;i<=n;i++)
{
dis[i]=0;
line[i].clear();
}
for (int i=1; i<=m; i++)
{
int A,B;
double Rab,Cab,Rba,Cba;
scanf("%d %d %lf %lf %lf %lf",&A,&B,&Rab,&Cab,&Rba,&Cba);
node now;
now.to=B;
now.R=Rab;
now.C=Cab;
line[A].push_back(now);
now.to=A;
now.R=Rba;
now.C=Cba;
line[B].push_back(now);
}
dis[s]=v;
dijkstra();
if (dis[s]<=v)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return 0;
}