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求环)的更多相关文章

  1. 【最短路】 ZOJ 1544 Currency Exchange 推断负圈

    给出 N 种货币 M 条兑换关系 開始时全部的货币S 和有X 块钱 接下来M条关系 A B W1 W2 W3 W4 表示 A->B 所需的手续费为W2块钱  汇率为W1 B->A 所需的手 ...

  2. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  3. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  4. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  5. (最短路 SPFA)Currency Exchange -- poj -- 1860

    链接: http://poj.org/problem?id=1860 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2326 ...

  6. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  8. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  9. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

随机推荐

  1. 20155238 实验四 Android程序设计

    Android 安装Android Studio 按照教程依次完成安装步骤.安装所存的相应文件夹必须纯英文,不能出现特殊字符. 32位系统和64位系统是同一个安装文件.启动程序中32位与64位都有.根 ...

  2. 【个人】爬虫实践,利用xpath方式爬取数据之爬取虾米音乐排行榜

    实验网站:虾米音乐排行榜 网站地址:http://www.xiami.com/chart  难度系数:★☆☆☆☆ 依赖库:request.lxml的etree (安装lxml:pip install ...

  3. Django实现websocket完成实时通讯、聊天室、在线客服等

    一 什么是Websocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在WebS ...

  4. IT高管和易筋经的故事

    老板是我非常敬重的前领导之一,他的一些管理风格,也影响了后来我对技术团队的管理. 理想企业 什么是程序员理想的IT企业?公司里面有良好的同事关系,合理的产品需求和开发进度,最好老板懂点编程,这样公司更 ...

  5. binary 和 varbinary 用法全解

    在SQL Server中,使用数据类型 binary(n) 和 varbinary(n) 存储二进制数据,n是指字节数量: binary(n):固定长度为 n 字节,其中 n 值从 1 到 8,000 ...

  6. Java中Class类详解、用法及泛化

    Java中Class类及用法 Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识,即所谓的RTTI.这项信息纪录了每个对象所属的类.虚拟机通常使用运行时类型信息选准正确方 ...

  7. 使用pyspark模仿sqoop从oracle导数据到hive的主要功能(自动建表,分区导入,增量,解决数据换行符问题)

    最近公司开始做大数据项目,让我使用sqoop(1.6.4版本)导数据进行数据分析计算,然而当我们将所有的工作流都放到azkaban上时整个流程跑完需要花费13分钟,而其中导数据(增量)就占了4分钟左右 ...

  8. 查看Python的版本、内建方法和模块等内容的方法

    若想更好地应用Python帮助我们解决日常生活的问题,就必须了解清楚它的内建方法和模块等特性.相信不少同学在安装某个版本的Python后,对于内建方法之类都是一知半解,希望本文能帮助了解Python的 ...

  9. 机房ping监控 smokeping+prometheus+grafana

    一.前言 1.本监控方案主要由smokeping+promethues+grafana组成.smokeping主要数据采集,promethues作为数据存储,grafana数据展示 2.其实smoke ...

  10. Vue.js 相关知识(脚手架)

    1. vue-cli 简介 Vue-cli 是 vue的设计者,为提升开发效率而提供的一个脚手架工具,可通过vue-cli快速构造项目结构 2. vue-cli 安装步骤 安装npm 或 cnpm n ...