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 ...
随机推荐
- FZU - 1901 Period II (kmp)
传送门:FZU - 1901 题意:给你个字符串,让你求有多少个p可以使S[i]==S[i+P] (0<=i<len-p-1). 题解:这个题是真的坑,一开始怎么都觉得自己不可能错,然后看 ...
- python+requests爬取百度文库ppt
实验网站:https://wenku.baidu.com/view/c7752014f18583d04964594d.html 在下面这种类型文件中的请求头的url打开后会得到一个页面 你会得到如下图 ...
- Codeforces Beta Round #19 D. Points
Description Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a C ...
- net core启动报错Unable to configure HTTPS endpoint. No server certificate was specified
这是因为net core2.1默认使用的https,如果使用Kestrel web服务器的话没有安装证书就会报这个错 其实仔细看他的错误提示,其中有一句叫你执行一个命令安装证书的语句: dotnet ...
- PTA L1-006 连续因子【暴力模拟】
一个正整数N的因子中可能存在若干连续的数字.例如630可以分解为3*5*6*7,其中5.6.7就是3个连续的数字.给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列. 输入 ...
- acm 快速傅里叶变换的理解
A(x)=A4[0](x*x)+x*A4[1](x*x);x=1,w,w*w,w*w*wwi means w^in=4;w=w[4]result shuould bey[0]=A4[0](1*1)+1 ...
- node.js module.exports & exports & module.export all in one
node.js module.exports & exports & module.export all in one cjs const log = console.log; log ...
- React 权限管理
React 权限管理 react in depth JWT token access_token & refresh_token access token & refresh toke ...
- git whoami
git whoami $ git config --list $ git config --global --list # quit $ q $ git config user.name xgqfrm ...
- Android Studio & SDK & JDK & setting path
Android Studio & SDK & JDK & setting path https://developer.android.com/studio/intro/upd ...