链接: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. mysql优化(初学)

    写的时候遇到了SQL语句的优化问题,在网上搜了一些学习.http://blog.csdn.net/kennyrose/article/details/7532032 索引: 1.可以在这些列上创建索引 ...

  2. Easyui columns列图片移位问题!!!

    InitGrid: function () { $("#list").datagrid({ toolbar: '#tb', url: BanZhengXiaoLuSearch.Aj ...

  3. sed 命令

    使用sed操作: .个人博客的文件,只输出学生姓名 .txt .txt .只输出每个学生的url .txt .只输出个人博客里的学号 .txt .只输出个人博客中,两个字姓名的学生名 .txt .只输 ...

  4. linux通过挂载系统光盘搭建本地yum仓库的方法

    1.挂载光盘 [root@localhost ~]# mount /dev/cdrom /media/cdrom/ mount: /dev/sr0 写保护,将以只读方式挂载 /media下的cdrom ...

  5. xplan.sql(本脚本获取执行计划显示执行顺序)

    -- ---------------------------------------------------------------------------------------------- -- ...

  6. C++ 系列:socket 资料收集

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  7. 用 Blend 给Windows Phone 应用创建 示例数据

    前言  创建 示例数据(Sample Data) 是提高程序开发效率的一个很有效方法,有了它,我们调UI的时候就不必每次都运行应用,然后在手机上观看页面效果了,配合 “AlignmentGrid.pn ...

  8. 忘记XP密码的解决方案

    仅供教学与研究用,后果自负! !! USE AT YOUR OWN RISK !! !! ONLY FOR EDUCATIONAL PURPOSE !! 介绍 获取SYSTEM权限.测试通过. 进入G ...

  9. iOS---FMDB数据升级

    本人在这里重要强调一下!!! 看这里,看这里,看这里,重要的事说三遍. 本人在项目开发中,由于需求问题,不得不对已经建立好的数据库进行修改(添加字段),我就很随意的直接添加了对一个的字段,运行一下,数 ...

  10. WebAPI图片上传

    public Task<HttpResponseMessage> PostFormData() { // Check if the request contains multipart/f ...