POJ 1860 Currency Exchange (SPFA松弛)
题目链接:http://poj.org/problem?id=1860
题意是给你n种货币,下面m种交换的方式,拥有第s种货币V元。问你最后经过任意转换可不可能有升值。下面给你货币u和货币v,r1是u到v的汇率,c1是u到v的手续费,同理r2是v到u的汇率,c2是v到u的手续费。转换后的钱B = (转换之前的钱A - c) * r。
我用spfa做的,不断地松弛。要是存在正环,或者中间过程最初的钱升值了,就说明会升值。有负环的话,不满足松弛的条件,慢慢地就会弹出队列,也就不会升值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = ;
struct data {
int next , to;
double r , c;
}edge[MAXN * ];
int head[MAXN] , cont;
double d[MAXN]; void init(int n) {
for(int i = ; i <= n ; i++) {
head[i] = -;
d[i] = ;
}
cont = ;
} inline void add(int u , int v , double r , double c) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].r = r;
edge[cont].c = c;
head[u] = cont++;
} bool spfa(int s , double V) {
queue <int> que;
while(!que.empty()) {
que.pop();
}
que.push(s);
d[s] = V;
while(!que.empty()) {
int temp = que.front();
que.pop();
for(int i = head[temp] ; ~i ; i = edge[i].next) {
double x = edge[i].r * (d[temp] - edge[i].c);
if(x > d[edge[i].to]) { //松弛
d[edge[i].to] = x;
que.push(edge[i].to);
if(d[s] > V) //增加则直接返回true
return true;
}
}
}
return false;
} int main()
{
int n , m , s , u , v;
double V , r , c;
while(~scanf("%d %d %d %lf" , &n , &m , &s , &V)) {
init(n);
for(int i = ; i < m ; i++) {
scanf("%d %d %lf %lf" , &u , &v , &r , &c);
add(u , v , r , c);
scanf("%lf %lf" , &r , &c);
add(v , u , r , c);
}
if(spfa(s , V))
printf("YES\n");
else
printf("NO\n");
}
}
POJ 1860 Currency Exchange (SPFA松弛)的更多相关文章
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)
链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...
- POJ 1860 Currency Exchange【SPFA判环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
- (简单) POJ 1860 Currency Exchange,SPFA判圈。
Description Several currency exchange points are working in our city. Let us suppose that each point ...
随机推荐
- linux delete files older than 3 days
4 down vote accepted This is easy enough (although note that this goes by a modification time more t ...
- MongoDB入门分享-笔记整理精选
最近在学习MongoDB,怕以后忘记,自己做了一个整理,给不知道的小伙伴一起分享学习一下. 第一步> 首先到官网下载,安装MongoDB.(注意MongoDB还有一个可视化管理工具叫: Mong ...
- BrowserSync,调试利器--自动刷新(转
---恢复内容开始--- 请想象这样一个场面:你开着两个显示器,一边是IDE里的代码,另一边是浏览器里的你正在开发的应用.此时桌上还放着你的手机,手机里也是这个开发中的应用.然后,你新写了一小段代码, ...
- Top Android App使用的组件 3
8684公交 AdChina:com.adchina:易传媒广告平台 AdsMogo:com.adsmogo:芒果移动广告平台 大姨吗 AChartEngine:org.achartengine:An ...
- Asp.net 将DataTable 或者DataSet 转换为Json 格式
Web 开发中,将从数据库中取到的数据直接转换为 Json 格式的数据,在前台通过Ajax 无刷新显示在界面上,下面提供将DataTable 或者DataSet 转换为Json 的方法 /// < ...
- Hadoop中OutputFormat解析
一.OutputFormat OutputFormat描述的是MapReduce的输出格式,它主要的任务是: 1.验证job输出格式的有效性,如:检查输出的目录是否存在. 2.通过实现RecordWr ...
- MySQL数据库分布式事务XA优缺点与改进方案
1 MySQL 外部XA分析 1.1 作用分析 MySQL数据库外部XA可以用在分布式数据库代理层,实现对MySQL数据库的分布式事务支持,例如开源的代理工具:ameoba[4],网易的DDB,淘宝的 ...
- Android:实现一种浮动选择菜单的效果
总结如何实现Android浮动层,主要是dialog的使用. 自定义一个类继承自Dialog类,然后在构造方法中,定义这个dialog的布局和一些初始化信息. 案例1: public class Me ...
- hdu 2087-剪花布条(KMP)
题意: 求文本串最多可以分成几个模式串. 分析: KMP #include <map> #include <set> #include <list> #includ ...
- IOS文章地址暂时记录
动画 http://www.jianshu.com/p/1c6a2de68753 iOS App性能优化 http://www.hrchen.com/2013/05/performance-wit ...