[poj1860] Currency Exchange (bellman-ford算法)

题目链接:http://poj.org/problem?id=1860

题目大意:给你一些兑换方式,问你能否通过换钱来赚钱?

使用ford算法,当出现赚钱的时候就返回YES,如果不能赚钱,则返回NO

应该是可以停下来的,但是我不会分析复杂度,谁来教教我?

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <map>
 6 #include <set>
 7 #include <bitset>
 8 #include <cmath>
 9 #include <numeric>
10 #include <iterator>
11 #include <iostream>
12 #include <cstdlib>
13 using namespace std;
14 #define PB push_back
15 #define MP make_pair
16 #define SZ size()
17 #define ST begin()
18 #define ED end()
19 #define CLR clear()
20 #define ZERO(x) memset((x),0,sizeof(x))
21 typedef long long LL;
22 typedef unsigned long long ULL;
23 typedef pair<int,int> PII;
24 const double EPS = 1e-8;
25
26 struct EDGE{
27     int from,to;
28     double r,c;
29     EDGE(int _from = 0, int _to =0 , double _r = 0, double _c = 0 ) :
30         from(_from),to(_to),r(_r),c(_c) {}
31 };
32
33 vector<EDGE> vec;
34 int N,M,S;
35 double V;
36 double d[111];
37
38 bool floyd(int s){
39     d[s] = V;
40     bool flag = false;
41     while(true){
42         flag = false;
43         for(int i=0;i<vec.SZ;i++){
44             const EDGE& e = vec[i];
45             if(d[e.from]>0.0 && d[e.to]<(d[e.from]-e.c)*e.r){
46                 d[e.to]=(d[e.from]-e.c)*e.r;
47                 flag = true;
48             }
49         }
50         if(d[s]>V) return true;
51         if(!flag) {
52             break;
53         }
54     }
55     return d[s]>V;
56 }
57
58
59 int main(){
60     while(EOF!=scanf("%d%d%d%lf",&N,&M,&S,&V)){
61         vec.CLR;
62         ZERO(d);
63         int from,to;
64         double rab,cab,rba,cba;
65         for(int i=0;i<M;i++){
66             scanf("%d%d%lf%lf%lf%lf",&from,&to,&rab,&cab,&rba,&cba);
67             vec.PB(EDGE(from,to,rab,cab));
68             vec.PB(EDGE(to,from,rba,cba));
69         }
70         puts(floyd(S)?"YES":"NO");
71     }
72     return 0;
73 }
时间: 2024-11-25 13:46:49

[poj1860] Currency Exchange (bellman-ford算法)的相关文章

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22405   Accepted: 8095 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

POJ1860 Currency Exchange【BellmanFord算法】【求正权回路】

Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20994 Accepted: 7522 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and perfor

POJ 1860 Currency Exchange(BellmanFord算法的变形,求正环)

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair o

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

Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力不从心了,而Bellman - Ford算法可以解决这种问题. Bellman - Ford 算法可以处理路径权值为负数时的单源最短路径问题.设想可以从图中找到一个环路且这个环路中所有路径的权值之和为负.那么通过这个环路,环路中任意两点的最短路径就可以无穷小下去.如果不处理这个负环路,程序就会永远运

Bellman—Ford算法思想

---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G运行Bellman—Ford算法的结果是一个布尔值,表明图中是否存在着一个从源点s可达的负权回路.若存在负权回路,单源点最短路径问题无解:若不存在这样的回路,算法将给出从源点s到图G的任意顶点v的最短路径值d[v] Bellman—Ford算法流程 分为三个阶段: (1)初始化:将除源点外的所有顶点

POJ1860——Currency Exchange(BellmanFord算法求最短路)

Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points sp

poj1860——Currency Exchange(Eellman-Ford+权值为正的环路)

Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the

POJ1860:Currency Exchange(BF)

http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be sev

POJ-1860 Currency Exchange 【spfa判负环】

Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the