Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 22648   Accepted: 8180

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

题意是给出了M种钱币,N个转换所,每个钱币的转换关系,中间要给转换所交一定的中介费。求是否能够有这样的条件,使得钱在转了一圈之后变多了。

看到只需输入YES/NO这样,不用求具体的单源点最短路径或是所有的,就感觉很符合Bellman。。。还有一点,Bellman和Floyd能够处理负权值的图,但Flyod不能有负环。Dijsktra不能处理含有负权值的图。这题用Bellman只是在给每条边松弛的时候不再是简单的相加了,而是dis[edge[j].s]-edge[j].com)*edge[j].l 。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct E{
int s;
int e;
double com;
double l;
}edge[5205]; int N,M,S,edge_num;
double V;
double dis[505]; void addedge(int start,int end,double co,double len)
{
edge_num++; edge[edge_num].s=start;
edge[edge_num].e=end;
edge[edge_num].com=co;
edge[edge_num].l=len;
} bool bellman_ford()
{
int i,j;
for(i=1;i<=N-1;i++)
{
int flag=0;
for(j=1;j<=edge_num;j++)
{
if(dis[edge[j].e]<(dis[edge[j].s]-edge[j].com)*edge[j].l)
{
flag=1;
dis[edge[j].e]=(dis[edge[j].s]-edge[j].com)*edge[j].l;
}
}
if(flag==0)
break;
} for(j=1;j<=edge_num;j++)
{
if(dis[edge[j].e]<(dis[edge[j].s]-edge[j].com)*edge[j].l)
return true;
} return false;
} int main()
{
int i,start,end;
double co,len; edge_num=0;
memset(dis,0,sizeof(dis)); cin>>N>>M>>S>>V; for(i=1;i<=M;i++)
{
cin>>start>>end>>len>>co;
addedge(start,end,co,len); cin>>len>>co;
addedge(end,start,co,len);
}
dis[S]=V;
if(bellman_ford())
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1860:Currency Exchange的更多相关文章

  1. 【POJ 1860】Currency Exchange

    [题目链接]:http://poj.org/problem?id=1860 [题意] 给你n种货币,m种货币之间的交换信息; 交换信息以 A,B,RA,CA,RB,CB的形式给出; 即A换B的话假设A ...

  2. (poj)1806 Currency Exchange

    题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...

  3. POJ 3903:Stock Exchange(裸LIS + 二分优化)

    http://poj.org/problem?id=3903 Stock Exchange Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. Currency Exchange POJ - 1860 (spfa)

    题目链接:Currency Exchange 题意: 钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式 ...

  5. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  6. poj 1860 Currency Exchange :bellman-ford

    点击打开链接 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16635   Accept ...

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

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

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

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

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

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

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-asterisk

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-pencil

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. Redis混合存储-冷热数据识别与交换

    Redis混合存储产品是阿里云自主研发的完全兼容Redis协议和特性的混合存储产品. 通过将部分冷数据存储到磁盘,在保证绝大部分访问性能不下降的基础上,大大降低了用户成本并突破了内存对Redis单实例 ...

  4. python三大神器===》生成器

    1. 认识生成器 利用迭代器,我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记录,进而才能根据当前状态生成下一 ...

  5. Git TortoiseGit github 操作

    由于公司采用了分布式架构,选择的是gitlab git 来管理代码等工作,鉴于github和gitlab的相似性,网上查看好多都是用git的命令上传,或者是一部分,为此,借鉴多方网络,并进行实际操作, ...

  6. Day4-T4

    原题目 Describe:建个图,连通后删边 [ 如果把 !dis[i][j] 全部定义为INF会更好理解 ] .先特判,再贪心求总数 code: #pragma GCC optimize(2) #i ...

  7. doc转docx

    # -*- coding: utf-8-*- import win32com from win32com.client import Dispatch w = win32com.client.Disp ...

  8. ActorFramework教程对比及规划

    牢骚太盛防肠断,风物长宜放眼量. 一.引子 昨天的文章,本来就是想写写ActorFramework的教程内容,结果写着写着偏了,变成了吐槽. 首先,声明一下,自己从未参加过任何LabVIEW培训班,也 ...

  9. oracle学习笔记(4)

    4.oracle数据库的启动流程 windows操作系统 启动监听: lsnrctl start; 启动数据库实例:oradim-startup-sid 实例名 linux系统 启动监听:lsnrct ...

  10. .net core项目iis10上出现 HTTP 错误 500.19,错误代码:0x8007000d

    文件权限更改,配置文件更改,IIS重装重启,各种办法都不管用,下面是解决办法: 看错误信息里:模块:IIS Web Core 打开IIS: 模块列表中是否缺少红框里的两个组件(我这是已经安装好的截图) ...