POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
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 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
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.
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
Http
POJ:https://vjudge.net/problem/POJ-1860
ZOJ:https://vjudge.net/problem/ZOJ-1544
Source
最短路径相关,求环
题目大意
求一个图中是否有正环
解决思路
运用spfa解决,我们用一个Tot记录每一个点进入队列的次数,如果发现某个点的Tot>n,则说明有环
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxN=201;
const int inf=2147483647;
class Data
{
public:
int v;
double R,C;
};
int n,m,S;
double V;
vector<Data> E[maxN];
double Dist[maxN];
bool inqueue[maxN];
int Tot[maxN];
queue<int> Q;
int main()
{
while(cin>>n>>m>>S>>V)
{
for (int i=1;i<=n;i++)
E[i].clear();
for (int i=1;i<=m;i++)
{
int u,v;
double Ruv,Cuv,Rvu,Cvu;
cin>>u>>v>>Ruv>>Cuv>>Rvu>>Cvu;
E[u].push_back((Data){v,Ruv,Cuv});
E[v].push_back((Data){u,Rvu,Cvu});
}
memset(Dist,0,sizeof(Dist));
memset(inqueue,0,sizeof(inqueue));
memset(Tot,0,sizeof(Tot));
while (!Q.empty())
Q.pop();
Dist[S]=V;
Q.push(S);
inqueue[S]=1;
bool get=0;
do
{
int u=Q.front();
//cout<<u<<endl;
//cout<<Dist[u]<<endl;
Q.pop();
inqueue[u]=0;
Tot[u]++;
if (Tot[u]>=n)
{
get=1;
cout<<"YES"<<endl;
break;
}
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i].v;
double w=(Dist[u]-E[u][i].C)*E[u][i].R;
if (w>Dist[v])
{
Dist[v]=w;
if (inqueue[v]==0)
{
Q.push(v);
inqueue[v]=1;
}
}
}
}
while (!Q.empty());
if (get==0)
cout<<"NO"<<endl;
}
return 0;
}
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)的更多相关文章
- 【最短路】 ZOJ 1544 Currency Exchange 推断负圈
给出 N 种货币 M 条兑换关系 開始时全部的货币S 和有X 块钱 接下来M条关系 A B W1 W2 W3 W4 表示 A->B 所需的手续费为W2块钱 汇率为W1 B->A 所需的手 ...
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- (最短路 SPFA)Currency Exchange -- poj -- 1860
链接: http://poj.org/problem?id=1860 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2326 ...
- POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】
链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
随机推荐
- 记录一次 @Autowired 无法注入( spring依赖正常 idea显示有spring已注入的图标)导致空指针异常的原因
首先,参考 https://blog.csdn.net/weixin_40475523/article/details/81085990 然后发现 是因为我把自己的这个类加上了 @Service 注解 ...
- [arc076F]Exhausted?[霍尔定理+线段树]
题意 地上 \(1\) 到 \(m\) 个位置摆上椅子,有 \(n\) 个人要就座,每个人都有座位癖好:选择 \(\le L\) 或者 \(\ge R\) 的位置.问至少需要在两边添加多少个椅子能让所 ...
- stl源码剖析 详细学习笔记 配接器
//---------------------------15/04/03---------------------------- /* 配接器概述: 1:adapter是一种设计模式:将一个clas ...
- C#调用python文件执行
我的电脑环境是使用.net framework4.5.1,如果在调试过程中调不通请注意 我用的是Visual studion 2017,python组件下载地址:http://ironpython.c ...
- ReactJS实用技巧(2):从新人大坑——表单组件来看State
不太清楚有多少初学React的同学和博主当时一样,在看完React的生命周期.数据流之后觉得已经上手了,甩开文档啪啪啪的开始敲了起来.结果...居然被一个input标签给教做人了. 故事是这样的:首先 ...
- Jmeter(十八)_Ubuntu部署jmeter与ant
Docker部署接口自动化持续集成环境第三步,容器化Jmeter与ant! 接上文:Docker_容器化jenkins 为了整合接口自动化的持续集成工具,我将jmeter与ant都部署在了Jenkin ...
- mount命令详解及常见问题汇总
一 .mount命令(用来挂载硬盘或镜像等) 用法:mount [-t vfstype] [-o options] device dir1.-t vfstype 指定文件系统的类型,通常不必指定.mo ...
- win10 添加项目右键用vscode打开
1.新建reg文件:在vscode安装目录下新建一个文本文件,然后将文件后缀改为:*.reg,文件名任意,例如:vsCodeOpenFolder.reg. 2.编写文本文件内容.将下面的内容Copy到 ...
- c语言数字图像处理(二):图片放大与缩小-双线性内插法
图像内插 假设一幅大小为500 * 500的图像扩大1.5倍到750 * 750,创建一个750 * 750 的网格,使其与原图像间隔相同,然后缩小至原图大小,在原图中寻找最接近的像素(或周围的像素) ...
- CentOS7使用winbind加入AD
https://ishm.idv.tw/?p=336 CentOS 7 使用 winbind 加入 AD 需求:已經熟悉 CentOS 6 的 AD 加入方式,CentOS 7 已將 winbind ...