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

题意是给你n种货币,下面m种交换的方式,拥有第s种货币V元。问你最后经过任意转换可不可能有升值。下面给你货币u和货币v,r1是u到v的汇率,c1是u到v的手续费,同理r2是v到u的汇率,c2是v到u的手续费。转换后的钱B = (转换之前的钱A - c) * r。

我用spfa做的,不断地松弛。要是存在正环,或者中间过程最初的钱升值了,就说明会升值。有负环的话,不满足松弛的条件,慢慢地就会弹出队列,也就不会升值。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = ;
struct data {
int next , to;
double r , c;
}edge[MAXN * ];
int head[MAXN] , cont;
double d[MAXN]; void init(int n) {
for(int i = ; i <= n ; i++) {
head[i] = -;
d[i] = ;
}
cont = ;
} inline void add(int u , int v , double r , double c) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].r = r;
edge[cont].c = c;
head[u] = cont++;
} bool spfa(int s , double V) {
queue <int> que;
while(!que.empty()) {
que.pop();
}
que.push(s);
d[s] = V;
while(!que.empty()) {
int temp = que.front();
que.pop();
for(int i = head[temp] ; ~i ; i = edge[i].next) {
double x = edge[i].r * (d[temp] - edge[i].c);
if(x > d[edge[i].to]) { //松弛
d[edge[i].to] = x;
que.push(edge[i].to);
if(d[s] > V) //增加则直接返回true
return true;
}
}
}
return false;
} int main()
{
int n , m , s , u , v;
double V , r , c;
while(~scanf("%d %d %d %lf" , &n , &m , &s , &V)) {
init(n);
for(int i = ; i < m ; i++) {
scanf("%d %d %lf %lf" , &u , &v , &r , &c);
add(u , v , r , c);
scanf("%lf %lf" , &r , &c);
add(v , u , r , c);
}
if(spfa(s , V))
printf("YES\n");
else
printf("NO\n");
}
}

POJ 1860 Currency Exchange (SPFA松弛)的更多相关文章

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

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

  2. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  3. POJ 1860 Currency Exchange 最短路+负环

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

  4. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  5. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  6. poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

    链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...

  7. POJ 1860 Currency Exchange【SPFA判环】

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

  8. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  9. (简单) POJ 1860 Currency Exchange,SPFA判圈。

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

随机推荐

  1. 函数os_file_pread

    /*******************************************************************//** Does a synchronous read ope ...

  2. 在ASP.NET中如何判断用户IE浏览器的版本

    f ( Request.Browser.MajorVersion == ) { // to do } ................................................. ...

  3. codevs 1171 潜伏者

    要是NOIP自己这样水就完了... 仔细啊!!!! #include<iostream> #include<cstdio> #include<cstring> #i ...

  4. 图解VS2010打包全过程

    原文转自:http://blog.csdn.net/shan9liang/article/details/6957308 最近刚刚打包发布了用VS2010开发的一个收费系统,借此讲一讲打包过程,供大家 ...

  5. <四>面向对象分析之UML核心元素之用例

    一:基本概念        --->用例定义了一组用例实例,其中每个实例都是系统所执行一系列操作,这些操作生成特定主角可以观测的值.        --->所谓用例,就是一件事情,要完成这 ...

  6. Android中FragmentPagerAdapter对Fragment的缓存(二)

    上一篇我们谈到了,当应用程序恢复时,由于FragmentPagerAdapter对Fragment进行了缓存的读取,导致其并未使用在Activity中新创建的Fragment实例.今天我们来看如何解决 ...

  7. 再一次见证mssql中in 与exist的区别

    见下面代码 /*+' select * from '+@strDBName +'.dbo.m_aic where nodeid not in(select nodeid from @tmpAIC) ' ...

  8. 【转】http-equiv的含义

    http-equiv顾名思义,相当于http的文件头作用,它可以向浏览器传回一些有用的信息,以帮助正确和精确地显示网页内容,与之对应的属性值为content,content中的内容其实就是各个参数的变 ...

  9. Linux iostat监测IO状态

    Linux iostat监测IO状态 http://www.orczhou.com/index.php/2010/03/iostat-detail/

  10. POJ 1312 Numerically Speaking

    题意:a = 1, b = 2, ..., z = 26, aa = 27, ... 给字符串或者数字,输出对应的答案. 解法:类似26进制……但又不完全是……拿java大数模拟了一下…… 代码: i ...