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 ...
随机推荐
- PowerShell随笔6---ISE
简单的命令可以通过控制台窗口输入执行,但是我的脚本逻辑复杂,需要保存.总不能在命令行工具中执行吧. 关了窗口,啥都没了.有没有一个IDE,有. 在PowerShell命令行窗口中输入:ISE,就会打开 ...
- k8s二进制部署 - etcd节点安装
下载etcd [root@hdss7-12 ~]# useradd -s /sbin/nologin -M etcd [root@hdss7-12 ~]# cd /opt/src/ [root@hds ...
- cmder设置方法
一.添加鼠标右键 Cmder.exe /REGISTER ALL 二.添加系统环境变量 我的电脑 > 右键属性 > 高级系统设置 > 环境变量 > 系统变量,在path中添加 ...
- Dapr是如何简化微服务的开发和部署
基于微服务设计模式的现代应用程序面临着一系列挑战.微服务需要有一个强大的服务发现机制来实现动态连接.它们需要松散耦合,实现自主性和独立缩放.微服务需要支持多种语言,其中每个服务都是以最合适的语言.框架 ...
- 【非原创】LightOj 1248 - Dice (III)【几何分布+期望】
学习博客:戳这里 题意:有一个 n 面的骰子,问至少看到所有的面一次的所需 掷骰子 的 次数的期望: 第一个面第一次出现的概率是p1 n/n; 第二个面第一次出现的概率是p2 (n-1)/n; 第三个 ...
- CodeForces - 13E(分块)
Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for on ...
- codeforces 3D (非原创)
D. Least Cost Bracket Sequence time limit per test 1 second memory limit per test 64 megabytes input ...
- Keras 报错: Error when checking target: expected dense_4...
笔者此处是一个回归任务, 最后一层是: ... pred = Dense(1)(x) 在最后一个Dense层前加上x = Flatten()(x)即可.
- tfrecords转图片存储
import os import shutil import tensorflow as tf import time import sys import cv2 # 图片存放位置 PATH_RES ...
- CSS clip-path in action
CSS clip-path in action <!DOCTYPE html> <html lang="en"> <head> <meta ...