题目连接: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. Merkaartor,Marble,QGIS等等

    Merkaartor介绍 Merkaartor是Qt开发开源的OpenStreetMap(下简称osm)数据的编辑器,这里简单列出相关资源.方面基于osm数据的开发. Merkaartor支持osm地 ...

  2. MFC模板CArray及其派生类

    CArray及其派生类 1. 简介:访问方法及效率和普通的数组一样,比普通数组强大的功能是可以改变数组的大小.Array采用队列方式存储数据,因而其内部数据元素是以物理方式顺序排列的,所以检索.顺序执 ...

  3. IOS 可靠性测试 iosMonkey

    UI AutoMonkey UI AUtoMonkey是一款非常简单的IOS压力测试工具.通过它,你可以向ios设备发送滑动.拖动.旋转.甚至锁屏和解锁指令.原文github地址:https://gi ...

  4. TWinControl.DefaultHandler处理WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC消息的两个参数很有意思,两个都是传递句柄

    procedure TWinControl.DefaultHandler(var Message); begin then begin with TMessage(Message) do begin ...

  5. Android switch 中 case expressions must be constant expressions 错误

    刚才导入android zxing 条码 的demo测试,发现出现如下错误 case expressions must be constant expressions 经检查,项目被设置成librar ...

  6. javascript中apply和eval结合的强大用法

        eval是一个函数,可以接受一个参数,这个参数可以作为js语句被解释性的执行,利用这个特性,eval和apply结合起来,可以大大简化代码  如下例子 <a class="cl ...

  7. 11661 - Burger Time?

      Burger Time?  Everybody knows that along the more important highways there are countless fast food ...

  8. 《Java从入门到精通》src0-8

    public class HelloWorld { public static void main(String[] args) { System.out.println("Hello wo ...

  9. 红眼技术博客 » redis连接池红眼技术博客 » redis连接池

    红眼技术博客 » redis连接池 redis连接池

  10. tc3162目录

    lsbin      dev      lib      proc     tmp      usrboaroot  etc      linuxrc  sbin     userfs   var# ...