链接: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. apk反编译工具

    反编译工具: apktool:资源文件获取,可以提取出图片文件和布局文件进行使用查看 dex2jar:将apk反编译成Java源码(classes.dex转化成jar文件) jd-gui:查看APK中 ...

  2. CString转换为string

    string CStringToString(CString strMFC) { CStringA strA; strA = strMFC.GetBuffer(); strMFC.ReleaseBuf ...

  3. 用flex做垂直居中

    <div class="flex-cont flex-centerbox"> <div class="center-cont"> < ...

  4. vi编辑器命令

    Linux下的文本编辑器有很多种,vi 是最常用的,也是各版本Linux的标配.注意,vi 仅仅是一个文本编辑器,可以给字符着色,可以自动补全,但是不像 Windows 下的 word 有排版功能.v ...

  5. scala eclipse plugin 插件安装

    最近在看Apache Apollo 代码,其中有很多scala代码,没办法需要安装一个scala插件. 我试过zip 安装,直接下载的update-site.zip 不能直接安装到位.我又特别懒,不想 ...

  6. 【HTML5】 web上的音频

    <!-- audio通过属性的设置可以控制音频播放的行为: 表6-2 audio元素的属性 ——————————————————————————————————————————————————— ...

  7. Java 反射调用动态方法

    package com.pigetest.util; import java.lang.reflect.Method; public class PrivateMethodTestHelper { p ...

  8. Android 登录界面与首页的设计

    全屏效果 //取消标题,取消状态栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(Wind ...

  9. Knockout.js随手记(8)

    visible, disable, css绑定 这个例子非常简单,主要演示如何通过属性控制html元素的显示与否(visible),可用性(disable)以及根据属性添加相应的CSS样式. 先简单的 ...

  10. Axure 自适应视图

    假设B为A的子视图 继承: A更新 文字内容.交互事件.禁用: 位置.尺寸.样式.交互样式 时, B都会继承响应更新变化 B更新 文字内容.交互事件.禁用时,A也会更新 B更新 位置.尺寸.样式.交互 ...