当前位置:网站首页>POJ1860货币兑换题解
POJ1860货币兑换题解
2022-07-28 12:37:00 【bj_hacker】
题目
链接
http://poj.org/problem?id=1860
字面描述
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
思路
用Bellman-ford判断是否有正环,也就是n-1次松弛后,还能再进行一次松弛则有正环,否则无。
代码实现
#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;
}
边栏推荐
- 今日睡眠质量记录75分
- What if the server cannot be connected (the original server cannot find the target resource)
- 不用Swagger,那我用啥?
- 验证码暴力破解测试[通俗易懂]
- Force buckle 2354. Number of high-quality pairs
- Leetcode 笔记 566. 重塑矩阵
- Extended operator
- Humiliation, resistance, reversal, 30 years, China should win Microsoft once
- Leetcode · daily question · 1331. array sequence number conversion · discretization
- Guide for using IP phone system and VoIP system
猜你喜欢

《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库

Shell basic concepts and variables

用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历

持续(集成--&gt;交付--&gt;部署)

Leetcode · daily question · 1331. array sequence number conversion · discretization

Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development

倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展

不用Swagger,那我用啥?

少儿编程 电子学会图形化编程等级考试Scratch二级真题解析(判断题)2022年6月

比XShell更好用、更现代的终端工具!
随机推荐
JS method of splitting strings
I copied the bottom of the liquidated NFT, but was locked by opensea
powerdesigner创建数据库模型(概念模型举例)
酷炫操作预热!代码实现小星球特效
Jenkins -- continuous integration server
微念“失去”李子柒的这一年
严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数
GameStop熊市杀入NFT交易,老牌游戏零售商借Web3焕发第二春
C language: optimized merge sort
Cesium pit -- pit used by various API calls and API itself
IP电话系统和VoIP系统使用指南
leetcode-136.只出现一次的数字
Some thoughts on.Net desktop development
Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
Resolve browser password echo
【架构】评分较高的三本微服务书籍的阅读笔记
[ecmascript6] symbol and its related use
Leetcdoe-342. Power of 4
Leetcode notes 566. Reshaping the matrix
Leetcode · daily question · 1331. array sequence number conversion · discretization