POJ-1860(最短路问题,Bellman-Ford算法判正圈)
Currency Exchange
POJ-1860
- 这题其实是最短路问题的变形,但是这里不用求解最短路,而是求解路径中是否存在正圈。如果存在正圈则说明兑换后的货币可以一直增加,否则不能实现通过货币转化来增加财富。
- 这和经典的使用Bellman-Ford判断是否存在负权也有不同的地方,这里需要在松弛方程中,改变判断的条件。
package POJ;
import java.util.*;
public class POJ_1860 {
static int n,m,s;
static double money;
static int edges;//边的数量
static class edge{
public int from,to;
public double rate,commisions;
edge(){}
edge(int from,int to,double rate,double commisions){
this.from=from;this.to=to;this.rate=rate;this.commisions=commisions;
}
};
static edge []es;
static double []d;
static boolean BellmanFord() {
d[s]=money;
for(int i=1;i<n;i++) {//下标从1开始
boolean flag=false;
for(int j=0;j<edges;j++) {
edge e=es[j];
if(d[e.to]<(d[e.from]-e.commisions)*e.rate) {
flag=true;
d[e.to]=(d[e.from]-e.commisions)*e.rate;
}
}
if(!flag)
return false;
}
for(int j=0;j<edges;j++) {
edge e=es[j];
if(d[e.to]<(d[e.from]-e.commisions)*e.rate) {
return true;
}
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
m=cin.nextInt();
s=cin.nextInt();
money=cin.nextDouble();
es=new edge[2*m];
d=new double[n+1];
int j=0;
for(int i=0;i<m;i++) {
int from,to;
double r,m,r1,m1;
from=cin.nextInt();to=cin.nextInt();r=cin.nextDouble();m=cin.nextDouble();r1=cin.nextDouble();m1=cin.nextDouble();
es[j++]=new edge(from,to,r,m);
es[j++]=new edge(to,from,r1,m1);
}
edges=j;
if(BellmanFord())
System.out.println("YES");
else System.out.println("NO");
}
}
``
POJ-1860(最短路问题,Bellman-Ford算法判正圈)的更多相关文章
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)
链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...
- POJ-3259(最短路+Bellman-Ford算法判负圈)
Wormholes POJ-3259 这题是最短路问题中判断是否存在负圈的模板题. 判断负圈的一个关键就是理解:如果在图中不存在从s可达的负圈,最短路径不会经过一个顶点两次.while循环最多执行v- ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
- Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】
题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...
- POJ 1860 Currency Exchange【SPFA判环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- (简单) POJ 1860 Currency Exchange,SPFA判圈。
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- poj1860 bellman—ford队列优化 Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22123 Accepted: 799 ...
随机推荐
- zjnu1709 UZASTOPNI (bitset,树形dp)
Description Petar is throwing a birthday party and he decided to invite some of the employees of his ...
- 数位dp整理 && 例题HDU - 2089 不要62 && 例题 HDU - 3555 Bomb
数位dp: 数位dp是一种计数用的dp,一般就是要统计一个区间[li,ri]内满足一些条件数的个数.所谓数位dp,字面意思就是在数位上进行dp.数位的含义:一个数有个位.十位.百位.千位......数 ...
- 梨子带你刷burp练兵场(burp Academy) - 服务端篇 - Sql注入配套漏洞讲解笔记
目录 Sql注入 什么是Sql注入呢? Sql注入有哪些例子? 检索隐藏数据 打破应用逻辑 利用Union进行跨库查询 如何确定利用Union的注入攻击所需的列数呢? 如何确定Union的查询结果中哪 ...
- kubernetes实战-配置中心(二)交付apollo配置中心到k8s
apollo官网:官方地址 apollo架构图: apollo需要使用数据库,这里使用mysql,注意版本需要在5.6以上: 本次环境mysql部署在10.4.7.11上,使用mariadb:10.1 ...
- 3.Work Queues
标题 : 3.Work Queues 目录 : RabbitMQ 序号 : 3 var channel1 = _connection.CreateModel(); channel1.BasicQos( ...
- 逆元 exgcd 费马小定理 中国剩余定理的理解和证明
一.除法取模逆元 如果我们要通过一个前面取过模的式子递推出其他要取模的式子,而递推式里又存在除法 那么一个很尴尬的事情出现了,假如a[i-1]=100%31=7 a[i]=(a[i-1]/2)%31 ...
- JVM终结篇
1.1 重新认知JVM 之前我们画过一张图,是从Class文件到类装载器,再到运行时数据区的过程.现在咱们把这张图不妨丰富完善一下,展示了JVM的大体物理结构图. 1.2 GC优化 内存被使用了之后, ...
- GitHub in depth
GitHub in depth GitHub 高级玩法 / 进阶教程 https://github.com/trending/dart?since=daily https://github.com/t ...
- js 检测浏览器开发者控制台是否被打开
var element = new Image(); Object.defineProperty(element, "id", { get: function () { debug ...
- c++ 使用进程id获取打开的网络端口
#pragma warning( disable : 4996) #include <winsock2.h> #include <ws2tcpip.h> #include &l ...