POJ1860-Currency Exchange-判正环
两种货币的交换可以当成两条边,建图后跑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-判正环的更多相关文章
- POJ-1860 Currency Exchange( Bellman_Ford, 正环 )
题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
- poj1860(spfa判正环)
题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...
- POJ-1860 Currency Exchange---Bellman-Ford判断正环
题目链接: https://vjudge.net/problem/POJ-1860 题目大意: 我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑 ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- poj1860 Currency Exchange(spfa判断是否存在正环)
题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加. 思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增 ...
- POJ1860 Currency Exchange【最短路-判断环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...
随机推荐
- [翻译] ASP.NET Core 利用 Docker、ElasticSearch、Kibana 来记录日志
原文: Logging with ElasticSearch, Kibana, ASP.NET Core and Docker 一步一步指导您使用 ElasticSearch, Kibana, ASP ...
- Redux与它的中间件:redux-thunk,redux-actions,redux-promise,redux-saga
序言 这里要讲的就是一个Redux在React中的应用问题,讲一讲Redux,react-redux,redux-thunk,redux-actions,redux-promise,redux-sag ...
- H5 标签选择器
08-标签选择器 我是段落 我是段落 我是段落 我是段落 我是段落 我是标题 <!DOCTYPE html> <html lang="en"> <he ...
- MySQL复制表的方式以及原理和流程
复制表的俩种方式: 第一.只复制表结构到新表 create table 新表 select * from 旧表 where 1=2 或者 create table 新表 like 旧表 第二.复制表结 ...
- js总结:JavaScript入门
1. JavaScript-输出内容(document.write) 2.在html中引用js文件 3.定义变量 语法:var 变量名 说明: ⑴变量必须使用字母.下划线(_)或者美元符($)开始. ...
- MySQL导出数据,并转存到Excel表格中
从数据库中导出数据的方法,这里就不提了,网上有很多方法,如果闲麻烦,可以看一下这个:mysql导出数据 其实使用最简单的下面这个语句: mysql > select * from demo in ...
- js文字从左边飞入效果
贴代码之前,我们先讲一下它的原理,我们使用setInterval,让文字一开始置于屏幕看不到的位置,左右上下都可以,然后让它的位置不断移入到屏幕看得到的位置. 下面上代码: html: <h2 ...
- js this的含义以及讲解
this关键字是一个非常重要的语法点.毫不夸张地说,不理解它的含义,大部分开发任务都无法完成. 首先,this总是返回一个对象,简单说,就是返回属性或方法“当前”所在的对象. 下面来两个例子来让大家更 ...
- react双组件传值和传参
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LLVM的安装
1. 官网下载 llvm 2. 官网下载cmake 3. configure 执行 llvm 发现报错 4. 解压缩 cmake 5.将cmake 下面的bin 目录放到环境变量里面去 6. 创建一个 ...