POJ1680 Currency Exchange SPFA判正环
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940
提示:关键在于反向利用Bellman-Ford算法
题目大意
有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加
货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的
怎么找正权回路呢?(正权回路:在这一回路上,顶点的权值能不断增加即能一直进行松弛)
题目分析:
一种货币就是图上的一个点
一个“兑换点”就是图上两种货币之间的一个兑换环,相当于“兑换方式”M的个数,是双边
唯一值得注意的是权值,当拥有货币A的数量为V时,A到A的权值为K,即没有兑换
而A到B的权值为(V-Cab)*Rab
本题是“求最大路径”,之所以被归类为“求最小路径”是因为本题题恰恰与bellman-Ford算法的松弛条件相反,求的是能无限松弛的最大正权路径,但是依然能够利用bellman-Ford的思想去解题。
因此初始化d(S)=V 而源点到其他店的距离(权值)初始化为无穷小(0),当s到其他某点的距离能不断变大时,说明存在最大路径
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=1e2+;
struct Edge{
int v,next;
double r,c;
}edge[N*];
int n,m,s,head[N],tot,cnt[N];
bool inq[N];
double d[N],v;
queue<int>q;
void add(int u,int v,double r,double c){
edge[tot].v=v;
edge[tot].r=r;
edge[tot].c=c;
edge[tot].next=head[u];
head[u]=tot++;
}
bool spfa(int s){
memset(d,,sizeof(d));
memset(inq,,sizeof(inq));
memset(cnt,,sizeof(cnt));
d[s]=v,q.push(s),inq[s]=true,cnt[s]=;
while(!q.empty()){
int u=q.front();
q.pop();
inq[u]=false;
for(int i=head[u];~i;i=edge[i].next){
int to=edge[i].v;
double r=edge[i].r,c=edge[i].c;
if(d[to]<r*(d[u]-c)){
d[to]=r*(d[u]-c);
if(!inq[to]){
inq[to]=true;
if(++cnt[to]>n)return true;
q.push(to);
}
}
}
if(d[s]>v)return true;
}
return false;
}
int main(){
scanf("%d%d%d%lf",&n,&m,&s,&v);
memset(head,-,sizeof(head)),tot=;
for(int i=;i<m;++i){
int u,v;
double r,c;
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))printf("YES\n");
else printf("NO\n");
return ;
}
POJ1680 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——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj1860(spfa判正环)
题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- loj 1221(spfa判正环)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25957 思路:由于路线为一个环,将路径上的权值改为c-p*d,那么 ...
- [模板]SPFA判负环
目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...
随机推荐
- Java添加事件的四种方式
Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动) /** * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器 * * @author ...
- json 包含字段及函数的写法
在javascript中写类有多种方式: 1.function()中嵌套function; 2.prototype的方式 ,3.json的方式,如下: <script language=&quo ...
- gwt 创建 超链接cell (HyperTextCell)
package com.cnblogs.hooligen.client; import com.google.gwt.cell.client.AbstractCell; import com.goog ...
- MSSql ID自动增长删除数据重1开始
dbcc checkident('db_Tome1.dbo.员工信息表',reseed,0) 注:dbcc checkident('表名',reseed,0)
- 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数
---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...
- OFBiz进阶之环境搭建(eclipse)
一. 环境准备 1. jdk1.6 下载地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive- ...
- mysql将多张表COUNT的数据相加
由于数据量过大,我们将根据用户id 将数据存储在不同的表中,根据用户id模10的余数作为表的后缀.有如下十张表:test_0, test_1, ... ,test_9现在需要根据某个条件查询统计数据我 ...
- 设置BootStrap导航条的高度
只要加上这段css就可以覆盖Bootstrap.css的代码,定制符合自己的样式 .navbar { min-height: 40px; } .nav > li > a { padding ...
- C#.net winform 控件和皮肤大全
1. 东日IrisSkin IrisSkin 共有两个版本,一个是IrisSkin.dll 用于.Net Framework1.0/1.1 和IrisSkin2.dll 用于.Net Framewor ...
- 自己写loader
http://www.cnblogs.com/lynxcat/archive/2013/03/08/2950373.html http://addyosmani.com/blog/building-a ...