Currency Exchange POJ - 1860 (spfa判断正环)
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.
Input
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.
Output
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开始拥有的货币数
给出M个兑换点的信息
a b 代表兑换的两种货币,然后给出a兑换b的汇率和佣金,b兑换a的汇率和佣金
问是否可以经过多轮兑换后使得本金变多 思路:spfa判断正环
两种方法 cnt【y】++ ,直到cnt【y】 >= n (y代表放入队列的点,也就是松弛点)
cnt【y】+=cnt【x】,直到cnt【y】>=n
当然这题也可以判断dist【v】 > V?(表示起点)
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; int n,m,s;
double v; struct Node
{
int y;
double val;
double sub;
int next;
Node(int y=,double val=,double sub = ,int next=):y(y),val(val),sub(sub),next(next) {}
} node[]; int cnt,head[];
void add(int x,int y,double val,double sub)
{
node[++cnt].y=y;
node[cnt].next=head[x];
node[cnt].val=val;
node[cnt].sub=sub;
head[x]=cnt;
}
queue<int>que;
double dist[];
int vis[];
int tot[];
int num[];
bool spfa()
{
while(!que.empty())que.pop();
memset(vis,,sizeof(vis));
memset(dist,,sizeof(dist));
memset(num,,sizeof(num));
que.push(s);
dist[s] = v;
while(!que.empty())
{ int from = que.front();
que.pop();
vis[from] = ;
for(int i=head[from]; i; i=node[i].next)
{
int to = node[i].y;
if(dist[to] < (dist[from]-node[i].sub)*node[i].val)
{
dist[to] = (dist[from]-node[i].sub)*node[i].val;
if(!vis[to])
{
que.push(to);
vis[to]=;
num[to]++;
if(num[to]>= n)return ;
}
}
}
// if(dist[s] > v)return 1;
}
return ;
} int main()
{
scanf("%d%d%d%lf",&n,&m,&s,&v);
for(int i=; i<=m; i++)
{
int u,v;
double a,b,c,d;
scanf("%d%d",&u,&v);
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
add(u,v,a,b);
add(v,u,c,d);
}
int ans = spfa();
if(ans)printf("YES\n");
else printf("NO\n");
}
Currency Exchange POJ - 1860 (spfa判断正环)的更多相关文章
- Currency Exchange POJ - 1860 spfa判断正环
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...
- Currency Exchange POJ - 1860 (spfa)
题目链接:Currency Exchange 题意: 钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式 ...
- poj 1860 (Bellman_Ford判断正环)
题意:给出n种货币,m中交换关系,给出两种货币汇率和手续费,求能不能通过货币间的兑换使财富增加. 用Bellman_Ford 求出是否有正环,如果有的话就可以无限水松弛,财富可以无限增加. #incl ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- poj3621 SPFA判断正环+二分答案
Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...
- (最短路 SPFA)Currency Exchange -- poj -- 1860
链接: http://poj.org/problem?id=1860 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2326 ...
- HDU 1317(Floyd判断连通性+spfa判断正环)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路
Description Several currency exchange points are working in our city. Let us suppose that each point ...
随机推荐
- (四) 虚拟摄像头vivi体验
目录 虚拟摄像头vivi体验 源码下载 修改Makefile 安装xawtv 测试体验 title: 虚拟摄像头vivi体验 date: 2019/4/23 19:20:00 toc: true -- ...
- TensorFlow windows 安装(base anaconda)
Python conda安装之后(19年默认是python3.7) 1.降级到python3.6 2.查看python版本 3.安装tensorflow
- 细说java系列之HashMap原理
目录 类图 源码解读 总结 类图 在正式分析HashMap实现原理之前,先来看看其类图. 源码解读 下面集合HashMap的put(K key, V value)方法探究其实现原理. // 在Hash ...
- 深入理解display属性——前端布局常用重要属性
小注:display属性在网页布局中非常常见,但经常用到的仅仅是block.inline-block.inline和none等寥寥几个属性值,下面本人将详细介绍display属性的各个方面 定义 di ...
- 第31月第25天 xcode debug 限制uitextfiled输入
1.xcode debug 了解了每个设置的意思,个人觉得对于一个普通的app来说可以这样配置这些设置: Generate Debug Symbols:DEBUG和RELEASE下均设为YES(和Xc ...
- 风火轮SMC532使用
2018年3月份申请了一个院创,要做一个基于NFC技术的考勤设备,想法是用手机的NFC将学号信息传导考勤机,由考勤机统计缺勤信息,因为自己的拖延症,一直拖到现在.现在一边写毕业论文一边准备院创答辩,又 ...
- Python自动化中的鼠标事件
1)form selenium.webdriver.common.action_chains import ActionChains 导入该模块 2)ActionChains(driver) :用于 ...
- PHP 常用知识点
@多台服务器共享 session 方案用户量很大,单台 Redis 根本就放不下怎么办?服务器端分布式存储了(Redis 集群. Memcached 集群),既然是分布式,那么就必须保证用户每次请求都 ...
- CNN卷积神经网络
import os # third-party library import torch import torch.nn as nn import torch.utils.data as Data i ...
- linux删除软链接文件【原创】
删除软链接文件时,不要加“/”,去注意 正确方法: rm -rf ./softlinkfile 错误方法: rm -rf ./softlinkfile/ 上面错误的操作,这样会把软链接的源文件一同删除 ...