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

时间:2023-08-30 17:25:41

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

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

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

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

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstdlib>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ size()
#define ST begin()
#define ED end()
#define CLR clear()
#define ZERO(x) memset((x),0,sizeof(x))
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const double EPS = 1e-; struct EDGE{
int from,to;
double r,c;
EDGE(int _from = , int _to = , double _r = , double _c = ) :
from(_from),to(_to),r(_r),c(_c) {}
}; vector<EDGE> vec;
int N,M,S;
double V;
double d[]; bool floyd(int s){
d[s] = V;
bool flag = false;
while(true){
flag = false;
for(int i=;i<vec.SZ;i++){
const EDGE& e = vec[i];
if(d[e.from]>0.0 && d[e.to]<(d[e.from]-e.c)*e.r){
d[e.to]=(d[e.from]-e.c)*e.r;
flag = true;
}
}
if(d[s]>V) return true;
if(!flag) {
break;
}
}
return d[s]>V;
} int main(){
while(EOF!=scanf("%d%d%d%lf",&N,&M,&S,&V)){
vec.CLR;
ZERO(d);
int from,to;
double rab,cab,rba,cba;
for(int i=;i<M;i++){
scanf("%d%d%lf%lf%lf%lf",&from,&to,&rab,&cab,&rba,&cba);
vec.PB(EDGE(from,to,rab,cab));
vec.PB(EDGE(to,from,rba,cba));
}
puts(floyd(S)?"YES":"NO");
}
return ;
}