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 ...
随机推荐
- SQL - 外链接和内连接
外链接和内连接: leetcode 题目:编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息: 第一次的答案:(错误) select ...
- 【layui】【jquery】通过layero获取iframe的元素
$(layero).find('iframe').contents().find('#id'); $(layero).find('iframe').contents().find('#jqGridY' ...
- 1.3.1 LVM条带化error
报错: #pvcreate /dev/dfb2 /dev/dfa2 /dev/dfc2 /dev/dfd2 already exists in filesystem Can't open /de ...
- 2019 CSP-J 游记(CQ LNBS考场 的退役之战)
T0.10 爆零之战 已经不是第一次参加NOIP了(哦,关于兔子也NOIP了) 这次比赛的话,感觉考场很温馨,键盘很舒适,老师很友善,下次还会来.(哦不,下次来不了了,即将提前退役[大雾]) 刚刚文化 ...
- Spring 通过@Import实现Bean的注册
今天看到一个神奇的用法, Spring可以通过@Import导入实现了ImportBeanDefinitionRegistrar接口的类来注册那个类. ImportBeanDefinitionRegi ...
- AntV设置Chart高度
AntV在使用柱状图时不能单独设置一个柱状图的宽度,但可以设置整体高度,宽度,代码如下 const chart = new F2.Chart({ id: 'c1', // 指定图表容器 ID widt ...
- Vue框架——页面组件中使用小组件
小组件在components文件夹中,页面组件在views文件夹中 一.先写小组件的vue,比如text.vue(在template设置模板渲染,style设置样式) <template> ...
- 唯一ID生成器--雪花算法
在微服务架构,分布式系统中的操作会有一些全局性ID的需求,所以我们不能用数据库本身的自增功能来产生主键值,只能由程序来生成唯一的主键值.我们采用的是twitter的snokeflake(雪花)算法. ...
- 引用和自包含令牌(Reference Tokens and Introspection)
访问令牌可以有两种形式:自包含的和引用的. 自包含令牌(Self-contained tokens): 使用受保护的.有时间限制的数据结构,该结构包含元数据,并声明通过网络传递用户或客户机的身份.一种 ...
- Deploy custom service on non hadoop node with Apache Ambari
1 I want to deploy a custom service onto non hadoop nodes using Apache Ambari. I have created a cu ...