当前位置:网站首页>Poj1860 currency exchange solution
Poj1860 currency exchange solution
2022-07-28 13:49:00 【bj_ hacker】
POJ1860 Currency exchange solution
subject
link
http://poj.org/problem?id=1860
Literal description
Currency Exchange
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 52661 Accepted: 20631
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
Source
Northeastern Europe 2001, Northern Subregion
Ideas
use Bellman-ford Judge whether there is a positive ring , That is to say n-1 After secondary relaxation , If you can relax again, there is a positive ring , Otherwise none .
Code implementation
#include<cstdio>
#include<string.h>
#include<cstring>
using namespace std;
const int maxn=1e4+10;
int n,m,s,cnt;
double v;
struct node{
int a,b;
double r,c;
}e[maxn];
double dis[maxn];
inline void add(int x,int y,double r,double c){
e[++cnt].a=x;
e[cnt].b=y;
e[cnt].r=r;
e[cnt].c=c;
}
inline bool Bellman_ford(){
memset(dis,0,sizeof(dis));
dis[s]=v;
for(int i=1;i<n;i++){
bool flag=false;
for(int j=1;j<=cnt;j++){
if(dis[e[j].b]<(dis[e[j].a]-e[j].c)*e[j].r){
dis[e[j].b]=(dis[e[j].a]-e[j].c)*e[j].r;
flag=true;
}
}
if(!flag)return false;
}
for(int i=1;i<=cnt;i++){
if(dis[e[i].b]<(dis[e[i].a]-e[i].c)*e[i].r)return true;
}
return false;
}
int main(){
scanf("%d%d%d%lf",&n,&m,&s,&v);
for(int i=1;i<=m;i++){
int x,y;
double a1,a2,b1,b2;
scanf("%d%d%lf%lf%lf%lf",&x,&y,&a1,&a2,&b1,&b2);
add(x,y,a1,a2);
add(y,x,b1,b2);
}
if(Bellman_ford())printf("YES\n");
else printf("NO\n");
return 0;
}
边栏推荐
- 7.依赖注入
- 30 day question brushing training (I)
- R language ggplot2 visualization: visualize the scatter diagram and add text labels to the data points in the scatter diagram, using geom of ggrep package_ text_ The rep function avoids overlapping da
- 【C语言】结构体指针与结构体变量作形参的区别
- Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
- SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
- JWT login authentication + token automatic renewal scheme, well written!
- org.apache.ibatis.exceptions.TooManyResultsException的异常排查过程
- JWT 登录认证 + Token 自动续期方案,写得太好了!
猜你喜欢

30天刷题计划(二)

30天刷题计划(四)

拒绝服务 DDoS 攻击

在 Kubernetes 中部署应用交付服务(第 1 部分)

7.依赖注入

30 day question brushing training (I)

【ECMAScript6】Promise

No swagger, what do I use?

Org.apache.ibatis.exceptions.toomanyresultsexception

SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
随机推荐
Deploy application delivery services in kubernetes (Part 1)
【C语言】结构体指针与结构体变量作形参的区别
word打字时后面的字会消失是什么原因?如何解决?
Humiliation, resistance, reversal, 30 years, China should win Microsoft once
LyScript 获取上一条与下一条指令
Uva11175 digraph D and E from D to e and back
Facial expression recognition based on pytorch convolution - graduation project "suggestions collection"
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
合并表格行---三层for循环遍历数据
30 day question brushing training (I)
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
[C language] the difference between structure pointer and structure variable as formal parameters
Rolling update strategy of deployment.
C language: optimized merge sort
To build agile teams, these methods are indispensable
GO语言-栈的应用-表达式求值
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
DOJP1520星门跳跃题解
30天刷题计划(三)
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing