题意:有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判断是否存在正环)的更多相关文章

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

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

  2. POJ1860 Currency Exchange —— spfa求正环

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

  3. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  4. vijos1053 用spfa判断是否存在负环

    MARK 用spfa判断是否存在负环 判断是否存在负环的方法有很多, 其中用spfa判断的方法是:如果存在一个点入栈两次,那么就存在负环. 细节想想确实是这样,按理来说是不存在入栈两次的如果边权值为正 ...

  5. Currency Exchange(判断是否有正环)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16456   Accepted: 5732 Description Seve ...

  6. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

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

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

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

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

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

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

随机推荐

  1. idea内置tomcat中java代码热更新

    按照上图设置后,然后修改代码后按shift+F9快捷键,即可实现代码更新,这时在debug模式下会看到代码变更后的输出

  2. Spring Boot JDBC:加载DataSource过程的源码分析及yml中DataSource的配置

    装载至:https://www.cnblogs.com/storml/p/8611388.html Spring Boot实现了自动加载DataSource及相关配置.当然,使用时加上@EnableA ...

  3. MF 模拟器读取PC串口数据

    using System; using Microsoft.SPOT; using Microsoft.SPOT.Input; using Microsoft.SPOT.Presentation; u ...

  4. ClickHouse中的循环复制集群拓扑

    关系型数据库,但千万级表关联数据库基本上不太可能做到秒出:考虑过Sharding,但数据量大, 各种成本都很高:热数据存储到ElasticSearch,但无法跨索引关联,导致不得不做宽表, 因为权限, ...

  5. redis常用命令及持久化机制

    redis  常用命令 查找redis服务文件 find / -name  redis-server 查找配置文件 find / -name redis.conf 启动服务时候,要指定配置文件 启动r ...

  6. IUrlHelper ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

    ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of ...

  7. js中常见的字符串方法(3)

    match() match()方法只接受一个参数,要么是一个正则表达式,要么是一个 RegExp 对象. 调用这个方法本质上与调用RegExp的exec()方法相同, var text = " ...

  8. Haystack搜索框架

    1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch,Whoosh ...

  9. VM配置Centos(第十三步分区设置)

    1.点击开启此虚拟机之后,选择第一个 (注意:如果鼠标不显示出来,按alt+ctrl键) 2.然后选择skip跳过检测,如果选择了ok就会有很长时间的检测 3.然后选择NEXT 4.选择中文,然后点击 ...

  10. epoll原理

    系统调用说明 epoll_create:在内核中创建epoll结构 epoll_ctl:add 1. 调用监听的文件的poll方法,设置callback 2. 设备就绪时唤醒等待队列上的进程,此时会调 ...