题目连接: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. 重操JS旧业第九弹:函数表达式

    函数表达式,什么概念,表达式中的函数表达式. 1 函数申明 function 函数名([函数参数]){ //函数体 } js中无论像这样的显示函数什么放在调用之前还是调用之后,都不影响使用,因为js解 ...

  2. spring mvc +cookie+拦截器功能 实现系统自动登陆

    先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...

  3. MFC为应用程序添加托盘(右键托盘,弹出菜单)

    源代码:http://download.csdn.net/detail/nuptboyzhb/4137784 1.       导入一个托盘图标的资源(.ico)格式:资源ID为IDI_ICON1 2 ...

  4. oracle维护表空间和数据文件

    1:重要参考 wiki 2: oracle doc 表空间参考 3:来自dba-oracle的参考 26,27,28,29 一:oracle 表空间概念 表空间是联系数据库的物理磁盘(数据文件)和逻辑 ...

  5. [置顶] ARM-Linux下WEB服务器Boa的移植、配置和运行测试

    Linux下使用的轻量级WEB服务器主要有:lighttpd.thttpd.shttpd和boa等等,而Boa是使用最为广泛的轻量级WEB服务器之一(当然,阿帕奇是世界使用排名第一的Web服务器软件) ...

  6. eval 捕获dbi错误

    [root@dr-mysql01 ~]# cat t2.pl use DBI; my $dbUser='zabbix'; my $user="root"; my $passwd=& ...

  7. 基于visual Studio2013解决面试题之0207单词翻转

     题目

  8. python实现PKCS5Padding

    python实现PKCS5Padding     python实现PKCS5Padding    2008-09-21     请参考    ssl-3-padding-mode    php的加密函 ...

  9. IOS 轻量级数据持久化 DataLite

    开发的过程中我们经常要保存一些配置信息,一般简单的是用 NSUserDefaults [[NSUserDefaults standardUserDefaults] objectForKey:key]; ...

  10. 【分享】深入浅出WPF全系列教程及源码

    本人10月份提出离职,可是交接非常慢,预计年底才会交接完,趁着交接之际,自学了一下WPF,由于这是微软未来的发展趋势,自WIN7以来包含前不久公布的WIN8,核心还是WPF,在此,将自己的学习成果做一 ...