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种货币,有m个兑换点,每个兑换点可以在收取Cab(币种为A)佣金(佣金固定!)后将A按Rab汇率换成B,也可在收取Cba(币种为B)佣金后将B按Rab汇率换成A,你现在有币种为S(我就不说S币了)的货币V元
请问你是否可以通过某些兑换将S换成其他货币,再换回S以获得更多的钱?
题解:很明显的一道spfa求正环,bfs方法中只需要一个点进队超过n次即构成正环
求正环吗,自然求的是最长路,而且d[x]+w>y也要改成d[x]*w>y,这也是常识了
代码如下:
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int x;
double r,c;
}; vector<node> g[];
double d[],c;
int vis[],cnt[];
int n,m,s; int check(int u)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
d[i]=;
}
d[u]=c;
queue<int> q;
q.push(u);
while(!q.empty())
{
int x=q.front();
int sz=g[x].size();
vis[x]=;
q.pop();
for(int i=;i<sz;i++)
{
int y=g[x][i].x;
double r=g[x][i].r,c=g[x][i].c;
if((d[x]-c)*r>d[y])
{
d[y]=(d[x]-c)*r;
cnt[y]++;
if(!vis[y])
{
q.push(y);
vis[y]=;
if(cnt[y]==n)
{
return ;
}
}
}
}
}
return ;
} int main()
{
scanf("%d%d%d%lf",&n,&m,&s,&c);
for(int i=;i<=m;i++)
{
int f,t;
double rab,cab,rba,cba,c1,c2;
scanf("%d%d%lf%lf%lf%lf",&f,&t,&rab,&cab,&rba,&cba);
node hehe;
hehe.x=t;
hehe.r=rab;
hehe.c=cab;
g[f].push_back(hehe);
hehe.x=f;
hehe.r=rba;
hehe.c=cba;
g[t].push_back(hehe);
}
int ans=check(s);
if(ans)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}


poj1860 Currency Exchange(spfa判断正环)的更多相关文章

  1. POJ1860 Currency Exchange —— spfa求正环

    题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  2. poj1860 Currency Exchange(spfa判断是否存在正环)

    题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加. 思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增 ...

  3. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

  4. POJ1680 Currency Exchange SPFA判正环

    转载来源:優YoU  http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...

  5. Currency Exchange POJ - 1860 (spfa判断正环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  6. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  7. poj3621 SPFA判断正环+二分答案

    Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...

  8. HDU 1317(Floyd判断连通性+spfa判断正环)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

随机推荐

  1. Kaggle 比赛项目总结(项目流程)

    一.EDA(Exploratory Data Analysis) EDA:也就是探索性的分析数据 目的: 理解每个特征的意义: 知道哪些特征是有用的,这些特征哪些是直接可以用的,哪些需要经过变换才能用 ...

  2. selenium定位方式源码的存放位置

    find_element方法源码存在位置 by定位方法

  3. 有趣的java小项目------猜拳游戏

    package com.aaa; //总结:猜拳游戏主要掌握3个方面:1.人出的动作是从键盘输入的(System.in)2.电脑是随机出的(Random随机数)3.双方都要出(条件判断) import ...

  4. 各种Java项目环境搭建-文档引用汇总记录

    springmvc环境搭建 1.如何用Maven创建web项目(具体步骤) 2.springmvc环境搭建,一步一步超简单

  5. selenium自动化浏览器后台运行headless模式

    通过selenium做WEB自动化的时候,必须要启动浏览器, 浏览器的启动与关闭会影响执行效率. 当我们在自己电脑运行代码时,还会影响做别的事情. 鉴于这种情况,Google针对Chrome浏览器新增 ...

  6. PHP中GD库的使用

    1.基本步骤 <?php /** * Created by PhpStorm. * User: jiqing * Date: 18-4-9 * Time: 上午9:34 * 熟悉步骤 */ // ...

  7. java成神之——集合框架之队列,栈,集合并发

    集合 队列和双端队列 PriorityQueue Deque BlockingQueue Queue 栈 集合并发 线程锁 线程安全集合 结语 集合 队列和双端队列 PriorityQueue 此队列 ...

  8. JAVA简单的文件I/O操作实例

    如果只是对文件进行普通的读写,可以不用文件流. 以下是实例: File file = new File("test1.txt"); //向文件写入数据的 PrintWriter p ...

  9. 生成ssl脚本文件

    read -p "Enter your domain [www.example.com]: " DOMAIN echo "Create server key...&quo ...

  10. Bootstrap教程目录

    1.Bootstrap 简介(Web前端CSS框架) 2.Bootstrap 学习资料 3.Bootstrap 入门 4.Bootstrap 概览 5.Bootstrap 栅格系统 6.Bootstr ...