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算法判正圈)的更多相关文章

  1. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  2. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  3. poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

    链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...

  4. POJ-3259(最短路+Bellman-Ford算法判负圈)

    Wormholes POJ-3259 这题是最短路问题中判断是否存在负圈的模板题. 判断负圈的一个关键就是理解:如果在图中不存在从s可达的负圈,最短路径不会经过一个顶点两次.while循环最多执行v- ...

  5. POJ 1860 Currency Exchange (bellman-ford判负环)

    Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...

  6. Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】

    题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...

  7. POJ 1860 Currency Exchange【SPFA判环】

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  8. (简单) POJ 1860 Currency Exchange,SPFA判圈。

    Description Several currency exchange points are working in our city. Let us suppose that each point ...

  9. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

随机推荐

  1. hdu5643 King's Game(约瑟夫环+线段树)

    Problem Description In order to remember history, King plans to play losephus problem in the parade ...

  2. java随机数的产生

    两种产生随机数的方法: 1.通过import java.util.Random来实现 2.Math.random() 一.第一种的话就是导入Random之后,先生成一个Random对象r,之后再利用r ...

  3. 1561: (More) Multiplication

    Description Educators are always coming up with new ways to teach math to students. In 2011, an educ ...

  4. Caocao's Bridges HDU - 4738 找桥

    题意: 曹操在赤壁之战中被诸葛亮和周瑜打败.但他不会放弃.曹操的军队还是不擅长打水仗,所以他想出了另一个主意.他在长江上建造了许多岛屿,在这些岛屿的基础上,曹操的军队可以轻易地攻击周瑜的军队.曹操还修 ...

  5. Codeforces Round #529 (Div. 3) F. Make It Connected (贪心,最小生成树)

    题意:给你\(n\)个点,每个点都有权值,现在要在这\(n\)个点中连一颗最小树,每两个点连一条边的边权为两个点的点权,现在还另外给了你几条边和边权,求最小权重. 题解:对于刚开始所给的\(n\)个点 ...

  6. .net中swagger忽略某些字段

    需要忽略的字段上用特性 [System.Text.Json.Serialization.JsonIgnore] 例如:

  7. codeforces 868C

    C. Qualification Rounds time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. 记录一个状压DP用到的骚操作

    不断的让i=i^lowbit(i)就可以枚举i二进制里面所有的1 嘛,很显然,怕是我没想到哦

  9. HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解

    题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...

  10. Monorepo All In One

    Monorepo All In One monorepos 只是一种思想,或设计模式,架构风格 https://trunkbaseddevelopment.com/monorepos/ Lerna h ...