两种货币的交换可以当成两条边,建图后跑Bellman_ford算法就好了。

Bellman_ford算法可以用来处理负边权,所以可以判断是否存在负环。反过来就可以判断是否存在正环。

 /*--------------------------------------------------------------------------------------*/
// Helica's header
// Second Edition
// 2015.11.7
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
/*--------------------------------------------------------------------------------------*/
using namespace std; const int maxn = +;
int N,M,T,S;
int tol;
double V; struct Edge
{
int u,v;
double c,r;
}E[*maxn]; double dist[maxn]; bool bellman_ford(int start,int n)
{
memset(dist,,sizeof dist);
dist[start] = V; for(int i=;i<n;i++)
{
bool flag = false;
for(int j=;j<tol;j++)if(dist[E[j].v] < (dist[E[j].u]-E[j].c)*E[j].r )
{
dist[E[j].v] = (dist[E[j].u]-E[j].c)*E[j].r;
flag = true;
}
if(!flag) return false;
} for(int j=;j<tol;j++)
{
if(dist[E[j].v] < (dist[E[j].u]-E[j].c)*E[j].r)
return true;
}
return false;
} int main()
{
while(~scanf("%d%d%d%lf",&N,&M,&S,&V))
{
int a,b;
double r1,r2,c1,c2;
tol = ;
for(int i=;i<M;i++)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
E[tol].u = a;E[tol].v = b;
E[tol].c = c1;E[tol++].r = r1;
E[tol].u = b;E[tol].v = a;
E[tol].c = c2;E[tol++].r = r2;
}
if(bellman_ford(S,N))
printf("YES\n");
else
printf("NO\n"); }
}

POJ1860-Currency Exchange-判正环的更多相关文章

  1. POJ-1860 Currency Exchange( Bellman_Ford, 正环 )

    题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...

  2. poj1860(spfa判正环)

    题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...

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

    题目链接: https://vjudge.net/problem/POJ-1860 题目大意: 我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑 ...

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

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

  5. poj1860 Currency Exchange(spfa判断正环)

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

  6. POJ1860 Currency Exchange —— spfa求正环

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

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

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

  8. POJ1860 Currency Exchange【最短路-判断环】

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

  9. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  10. POJ1860——Currency Exchange(BellmanFord算法求最短路)

    Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...

随机推荐

  1. 性能调优3:硬盘IO性能

    数据库系统严重依赖服务器的资源:CPU,内存和硬盘IO,通常情况下,内存是数据的读写性能最高的存储介质,但是,内存的价格昂贵,这使得系统能够配置的内存容量受到限制,不能大规模用于数据存储:并且内存是易 ...

  2. 最新版XCoder 的使用方法

    1.项目中,新建一个类库.名字随意,我取名:XCoder 2.右键 > 管理nuget程序包:搜索 XCode 并安装 3.在项目中新建:data.project.xml 的xml文件,并写入数 ...

  3. 关于VS2017 添加 EF的MVC控制器报错的解决方法

    1. 错误描述 :no database provider has been configured fot this DbContext. 此类错误是上下文的注册造成的.解决方式在DBContext中 ...

  4. python 可调用对象之类实例

    可调用对象,即任何可以通过函数操作符()来调用的对象. python可调用对象大致可以分为4类: 1.函数 python中有三种函数:内建函数(BIFs).用户自定义函数(UDF).lambda表达式 ...

  5. 一些leetcode算法题

    DFS算法 思想:一直往深处走,直到找到解或者走不下去为止 DFS(dep,...) // dep代表目前DFS的深度 { if (找到解或者走不下去了){ return; } 枚举下种情况,DFS( ...

  6. case when then的用法-leetcode交换工资

    case具有两种格式:简单case函数和case搜索函数. --简单case函数 case sex when ' then '男' when ' then '女’ else '其他' end --ca ...

  7. mybatis的mapper注入失败

    因为处在两个不同的资源文件夹下: 导致classpath无法加载其中一些文件,所以修改为classpath*后顺利进行. <!-- 加载spring容器 --> <!-- neede ...

  8. Tomcat集成Memcached Session Manager方案

    http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.2/memcached-session-ma ...

  9. Error Boundaries 错误边界

    错误边界是用于捕获其子组件树 JavaScript 异常,记录错误并展示一个回退的 UI 的 React 组件,而不是整个组件树的异常.错误边界在渲染期间.生命周期方法内.以及整个组件树构造函数内捕获 ...

  10. Servlet--HttpServlet实现doGet和doPost请求的原理

    转:https://blog.csdn.net/m0_38039437/article/details/75264012 一.HttpServlet简介 1.HttpServlet是GenericSe ...