POJ 1860:Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 22648 | Accepted: 8180 |
Description
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
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
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的更多相关文章
- 【POJ 1860】Currency Exchange
[题目链接]:http://poj.org/problem?id=1860 [题意] 给你n种货币,m种货币之间的交换信息; 交换信息以 A,B,RA,CA,RB,CB的形式给出; 即A换B的话假设A ...
- (poj)1806 Currency Exchange
题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
- POJ 3903:Stock Exchange(裸LIS + 二分优化)
http://poj.org/problem?id=3903 Stock Exchange Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- Currency Exchange POJ - 1860 (spfa)
题目链接:Currency Exchange 题意: 钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式 ...
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
- poj 1860 Currency Exchange :bellman-ford
点击打开链接 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16635 Accept ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- (最短路 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 ...
随机推荐
- JuJu团队11月27号工作汇报
JuJu团队11月27号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 于达 将真实数据处理后按矩阵读入, 以供训练使用 提供generator的接口 对julia语言还不够 ...
- Day 31:CSS选择器、常用CSS样式、盒子模型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- asp.net增加指定404页面
对于在asp中添加404页面我并不熟悉,所以刚开始只能在网上找各种资料,网上资源太多,各种借鉴. 1.借鉴网上尝试的第一种方法: 首先,修改应用程序根目录的设置,打开 “web.config” ...
- 如何禁用AD OU 下面的邮箱用户的Exchange ActiveSync 和 适用于设备的OWA
Get-Mailbox -OrganizationalUnit QQ禁用名单 | Set-CASMailbox -ActiveSyncEnabled $false -OWAforDevicesEna ...
- linux下安装mysql5.7(centos6.0)
注:因为网络原因,这个mysql安装是我以前在学校的时候找到的一个安装包,不过也找到了下载的地址:http://www.itmop.com/downinfo/143061.html下载完成后,把文件上 ...
- 6 —— node —— 响应一个完整的页面
const http = require('http'); const fs = require('fs'); const server = http.createServer(); server ...
- 查看linux硬件的信息
cpu: cat /proc/cpuinfo 内存: cat /proc/meminfo 查看内存使用情况: free -m -m指以M的单位显示 查看硬盘使用情况: df -h ...
- 4. Retrieving a mapper(检索映射器)
Retrieving a mapper(检索映射器) 4.1. The Mappers factory(映射工厂) 可以通过 org.mapstruct.factory.Mappers 类检索映射器实 ...
- HDOJ 1722--Cake(切蛋糕问题)
一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食. Input 每行有两个数p和q ...
- springboot - 映射 /error 到自定义且实现了ErrorController的Controller
1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...