链接:http://poj.org/problem?id=1860

Currency Exchange

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

经过n-1次循环还能继续增大的情况说明有环,就一定能达到足够大,可以在回到起始点的时候还能增值
代码:
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
double dist[MAXN];
struct Edge
{
int u,v;
double r,c;
};
vector<Edge> E;
bool bellman_ford(int start,int n,double val)//点的编号从1开始
{
for(int i=;i<=n;i++)dist[i]=;
dist[start]=val;
for(int i=;i<n;i++)//最多做n-1次
{
bool flag=false;
for(int j=;j<E.size();j++)
{
int u=E[j].u;
int v=E[j].v;
double r=E[j].r;
double c=E[j].c;
if(dist[v]<(dist[u]-c)*r)
{
dist[v]=(dist[u]-c)*r;
flag=true;
}
}
if(!flag)return false;
}
for(int j=;j<E.size();j++)
if(dist[E[j].v]<(dist[E[j].u]-E[j].c)*E[j].r)
return true;
return false;
}
int main()
{
int n,m;
int start;
double val;
int a,b;
int i;
double r,c;
Edge e;
scanf("%d%d%d%lf",&n,&m,&start,&val);
E.clear();
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
scanf("%lf%lf",&r,&c);
e.u=a;e.v=b;e.r=r;e.c=c;
E.push_back(e);
scanf("%lf%lf",&r,&c);
e.u=b;e.v=a;e.r=r;e.c=c;
E.push_back(e);
}
if(bellman_ford(start,n,val))
printf("YES\n");
else
printf("NO\n");
return ;
}

POJ1860 Currency Exchange(bellman-ford)的更多相关文章

  1. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  2. Currency Exchange(最短路)

    poj—— 1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29851   Ac ...

  3. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

  4. POJ 1860 Currency Exchange(SPFA+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  5. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  6. poj1860(Bellman—fold)

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

  7. POJ 1860 Currency Exchange (bellman-ford判负环)

    Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...

  8. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  9. POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)

    题目链接: https://cn.vjudge.net/problem/POJ-1860 Several currency exchange points are working in our cit ...

随机推荐

  1. lnmp安装

    一.准备工作 需要的安装包都是从官网下载的,系统centos6.6 nginx-1.10.1.tar.gz php-5.6.24.tar.gz mysql-5.5.32.tar.gz 所有的包都一传入 ...

  2. Xcode 常用快捷键

    一.Xcode基本快捷键 1.1.新建项目 Shift + CMD + N 1.2.项目中新建文件 CMD + N 1.3.运行 CMD + R 1.4.编译 CMD + B 1.5.停止运行 CMD ...

  3. Java实现画八卦

    八卦是由多个圆叠加而成,如果我们让每个圆都有自己的颜色,那么具体结构便一目了然,如下图所示: 显然只要令对应的圆颜色相同,就能达到我们预期的效果. 用Java就能轻松画出来: import java. ...

  4. Web GIS离线解决方案

    1.背景 在离线环境下(局域网中)的GIS系统中如何使用地图?这里的地图主要指的是地图底图,有了底图切片数据,我们就可以看到地图,在上面加上自己的业务数据图层,进行相关操作. 要在离线环境下看到GIS ...

  5. 使用dd命令备份Linux分区

    为了备份分区,开始使用的是Remastersys,但最终生成的iso文件仅有几十K,应该是软件bug,且此软件不再更新,后尝试使用Linux Respin,但github一直连接不上. 其实可以尝试使 ...

  6. 3.4.4 数据预留和对齐(skb_reserve, skb_push, skb_put, skb_pull)

    转自:http://book.51cto.com/art/201206/345043.htm <Linux内核源码剖析:TCP/IP实现>本书详细论述了Linux内核2.6.20版本中TC ...

  7. webpack初试

    前言: 知道这完儿,没用过.关于webpack有很多介绍了,就不多说了.放几个链接,方便新手理解.这是给纯没用过的人了解的.这里只是简单介绍一下webpack的基本用法.大多内容都是来自webpack ...

  8. Hadoop 全分布模式 平台搭建

    现将博客搬家至CSDN,博主改去CSDN玩玩~ 传送门:http://blog.csdn.net/sinat_28177969/article/details/54138163 Ps:主要答疑区在本帖 ...

  9. SQL 语句与性能之执行顺序

    select * , t3.Name from t1 left join t2 on t1.sysno = t2.Asysno left join t3 on t3.sysno = t2.Bsysno ...

  10. 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)

    题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...