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

题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rate,现在问你有v钱,从s开始出发交换钱能不能赚钱。

分析:如何存在正环,能无限增加钱,肯定可以赚了,因此用spfa判一下即可

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-9
#define N 1010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
int v,next;
double R,C;
edge(){}
edge(int v,double R,double C,int next):v(v),R(R),C(C),next(next){}
}e[N<<];
int head[N<<],tot;
int vis[N],cnt[N];
int n,m,S;
double V,dis[N];
void init()
{
FILL(vis,);FILL(head,-);
FILL(dis,);FILL(cnt,);tot=;
}
void addedge(int u,int v,double r,double c)
{
e[tot]=edge(v,r,c,head[u]);
head[u]=tot++;
}
int spfa()
{
queue<int>que;
vis[S]=;cnt[S]=;
dis[S]=V;que.push(S);
while(!que.empty())
{
int u=que.front();
que.pop();vis[u]=;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
double w=(dis[u]-e[i].C)*e[i].R;
if(dis[v]<w)
{
dis[v]=w;
if(!vis[v])
{
vis[v]=;
cnt[v]++;
if(cnt[v]>n)return ;
que.push(v);
}
}
}
}
return ;
}
int main()
{
while(scanf("%d%d%d%lf",&n,&m,&S,&V)>)
{
init();
for(int i=;i<=m;i++)
{
int u,v;
double r1,c1,r2,c2;
scanf("%d%d%lf%lf%lf%lf",&u,&v,&r1,&c1,&r2,&c2);
addedge(u,v,r1,c1);
addedge(v,u,r2,c2);
}
if(spfa())puts("YES");
else puts("NO");
}
}

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

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

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

  2. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. POJ 2240 Arbitrage spfa 判正环

    d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...

  4. loj 1221(spfa判正环)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25957 思路:由于路线为一个环,将路径上的权值改为c-p*d,那么 ...

  5. POJ1680 Currency Exchange SPFA判正环

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

  6. [模板]SPFA判负环

    目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...

  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. poj1860 Currency Exchange(spfa判断正环)

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

  9. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

随机推荐

  1. MSSQL - 多表查询

    SELECT u.UserNumber, u.UserName, c.CarNumber, c.CarName, c.CarEngine, s.BuyLs, s.BuyTime FROM Tb_Sal ...

  2. Qt控件精讲一:按钮

    原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...

  3. RHEL4 i386下安装rdesktop【原创】

    http://rpmfind.net/ 1.根据系统下载rdesktop (1)查看Linux版本:# lsb_release -aLSB Version: :core-3.0-ia32:core-3 ...

  4. 基于visual Studio2013解决C语言竞赛题之1074八皇后

        题目 解决代码及点评 /************************************************************************/ /* ...

  5. STM32的FSMC总线复用调试笔记

    调试FSMC总线复用模式时主要遇到以下几点: 1.寄存器的配置,首先注意使能地址数据复用,其次要存储器类型选择FSMC_MemoryType_NOR,否则出现不了NADV信号. FSMC_NORSRA ...

  6. Swift - 使用Auto Layout和Size Classes实现页面自适应弹性布局

    在过去只有iphone4的时候,可以在代码里将一个可视单元的位置写死,这样是没问题的,但随着iPhone5,6的发布,屏幕尺寸有了越来越多种可能.这就要求App的UI控件具有在不同屏幕尺寸的设备上具有 ...

  7. gbs remotebuild使用说明

    本文件从:https://source.tizen.org/documentation/articles/gbs-remotebuild翻译而来. 1 远程构建 使用remotebuild子指令将本地 ...

  8. shell脚本中怎样同时执行多个.sql文件,并把结果写入文件中(转)

    转载:http://joewalker.iteye.com/blog/408879命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名;导出整个数据 ...

  9. python开发与实战content

    课时10_字典和集合 课时11 练习:列表运算.doc 课时12_讨论答疑:日志.引用.单例模式等 课时13_函数式编程 课时14 练习:编程实现若干函数.doc 课时15_练习讲解:编写函数 课时1 ...

  10. 将n进制的数组压缩成字符串(0-9 a-z)同一时候解压

    比如一个3进制的数组: [1 1 2 2 2 0 0] 用一个字符串表示... 此类题目要明白两点: 1. 打表:用数组下标索引字符.同一时候注意假设从字符相应回数字: int index = (st ...