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 个,问是否能通过合理地兑换货币,使得你 ...
随机推荐
- test zhenai
web.Document.InvokeScript("eval",new string[]{"document.getElementById('passwordbt'). ...
- 20155217《网络对抗》Exp02 后门原理与实践
20155217<网络对抗>Exp02 后门原理与实践 实验要求 使用netcat获取主机操作Shell,cron启动. 使用socat获取主机操作Shell,任务计划启动. 使用MSF ...
- python 网络爬虫介绍
一.网络爬虫相关概念 网络爬虫介绍 我们都知道,当前我们所处的时代是大数据的时代,在大数据时代,要进行数据分析,首先要有数据源,而学习爬虫,可以让我们获取更多的数据源,并且这些数据源可以按我们的目的进 ...
- 一步一步来熟悉Akka.Net(一)
一步一步来熟悉Akka.Net(一) 标签(空格分隔): .netcore 分布式 一.不利flag 好久没写过文章了,翻开前几年写的博客,看到有两个目标"代码生成器"和&qu ...
- GitHub 新手教程 六,Git GUI 新手教程(3),从GitHub远端同步代码库
从GitHub把代码库下载到本地: 1,打开 GitGUI,单击我们之前克隆好的本地库: 2,按图片所示点击,同步远端代码: 3,出现如下提示后,点击“Close”: 4,上面只是把代码下载下来,还没 ...
- OpenGL:使用顶点数组法绘制正六面体
在今天的opengl的课程以及实验中,我们学习了如何使用顶点数组的方法来绘制图形,但相信还有很多同学对它的实际使用方法不太了解,我们就用我们今天实验课上的实例来简单讲解一下 题目及要求 绘制一个正六面 ...
- Unity协程Coroutine使用总结和一些坑
原文摘自 Unity协程Coroutine使用总结和一些坑 MonoBehavior关于协程提供了下面几个接口: 可以使用函数或者函数名字符串来启动一个协程,同时可以用函数,函数名字符串,和Corou ...
- 从零系列--开发npm包(一)
一.目的 主要是纪录和回顾自己开发的一些步骤以及遇到的一些问题和解决方案 二.准备工作 1.IDE 选择 VS Code 2.安装node 环境 (https://nodejs.org/zh-cn/) ...
- 微软职位内部推荐-Senior PM
微软近期Open的职位: Senior Product Manager My Life & Work Beijing China Our passion is to enable people ...
- 20181023-3 每周例行报告(添加PSP)
此作业要求:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2100] 一.本周PSP表格 类型 任务 开始时间 结束时间 中断时间 净时 ...