poj1860 Currency Exchange(spfa判断是否存在正环)
题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加。
思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增加,最后的钱肯定也是增加的。
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std; const int maxn = + ;
struct edge{
int to, next;
double r, c;
} ed[maxn*];
int n, m, s, inq[maxn];
int head[maxn], tot;
double v, dis[maxn];
bool vis[maxn];
inline void add( int u, int v, double r, double c ){
ed[tot].to = v;
ed[tot].r = r;
ed[tot].c = c;
ed[tot].next = head[u];
head[u] = tot ++;
} inline bool spfa(int start){
queue<int> q;
memset( vis, , sizeof(vis) );
memset( dis, , sizeof(dis) );
memset( inq, , sizeof(inq) );
dis[s] = v;
vis[s] = ;
q.push(s);
while( q.size() ){
int u = q.front();
q.pop();
vis[u] = ;
if( ++inq[u]>=n ) return ; //一个点进队列的次数大于等于点的数量n则存在环
for( int i=head[u]; i!=-; i=ed[i].next ){
int v = ed[i].to;
if( dis[v]<(dis[u]-ed[i].c)*ed[i].r ){
dis[v] = (dis[u]-ed[i].c)*ed[i].r;
if( !vis[v] ){
vis[v] = ;
q.push(v);
}
}
}
}
return ;
} int main(){
// freopen("in.txt", "r", stdin);
scanf("%d%d%d%lf", &n, &m, &s, &v);
memset( head, -, sizeof(head) );
tot = ;
for( int i=; i<m; i++ ){
int u, v;
double ru, cu, rv, cv;
scanf("%d%d%lf%lf%lf%lf", &u, &v, &ru, &cu, &rv, &cv);
add( u, v, ru, cu );
add( v, u, rv, cv );
}
if( spfa(s) ) puts("YES");
else puts("NO"); return ;
}
poj1860 Currency Exchange(spfa判断是否存在正环)的更多相关文章
- 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 ...
- POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】
链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- vijos1053 用spfa判断是否存在负环
MARK 用spfa判断是否存在负环 判断是否存在负环的方法有很多, 其中用spfa判断的方法是:如果存在一个点入栈两次,那么就存在负环. 细节想想确实是这样,按理来说是不存在入栈两次的如果边权值为正 ...
- Currency Exchange(判断是否有正环)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16456 Accepted: 5732 Description Seve ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- POJ-1860 Currency Exchange( Bellman_Ford, 正环 )
题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...
- POJ1860 Currency Exchange【最短路-判断环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
随机推荐
- 量化编程技术—pdb进行调试
# -*- coding: utf-8 -*- # @Date: 2017-08-26 # @Original: import pdb def gen_buy_change_list(): buy_c ...
- Mac AXURE9 汉化
1.下载汉化文件 https://pan.baidu.com/s/1qE0ZSvf210WLMfvi8RlMpg 2.把lang文件放在Resources文件夹下 3.重新打开Axure就ok了
- SonarQube - 安装与运行SonarQube
1 - 下载SonarQube SonarQube有多个版本,其中CE(Community Edition)版本免费开源,其余的开发者版本.企业版本和数据中心版本都是收费版本. 官网下载:https: ...
- Google BERT摘要
1.BERT模型 BERT的全称是Bidirectional Encoder Representation from Transformers,即双向Transformer的Encoder,因为dec ...
- Apache Kafka® is a distributed streaming platform
Kafka Connect简介 我们知道过去对于Kafka的定义是分布式,分区化的,带备份机制的日志提交服务.也就是一个分布式的消息队列,这也是他最常见的用法.但是Kafka不止于此,打开最新的官网. ...
- C# vb .net图像合成-合成星形
在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...
- sqlserver 2005 数据库的差异备份与还原
找到一个可靠的步骤,点开链接:http://blog.csdn.net/kevindr/article/details/22154323
- Kubernetes Storage Persistent Volumes
链接:https://kubernetes.io/docs/concepts/storage/persistent-volumes/ 支持的参数,比如mountOptions在这里可以找到 删除正在被 ...
- int转换为String,常用的四种方法。
int i = 100; 方法一:String s1 = i + " "; 方法二:String s2 = String.valueof(i); 方法三(先转换为Integer类型 ...
- php数组的数学功能相关常用函数
php数组中有一些函数与数学相关的函数,大多都是以array开头然后下划线接一个数学上的英文单词,如下: array_diff() array_diff_assoc() array_intersect ...