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 of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
用bellman-ford不断进行松弛,当松弛V次时(顶点)说明图中有正权回路。
#include<iostream>
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string>
using namespace std;
#define N 1000+5
#define LL long long int
#define pow(a) ((a)*(a))
#define mem(arr,a) memset(arr,a,sizeof(arr))
struct edge{
int from, to;
double rate, tax;
};
int n, m, s;
double sum;
edge es[N];
int E = 0;
double d[N];
bool bellman(int s){
mem(d, 0);
d[s] = sum;
int cnt = 0;
while (1){
int v = -1;
cnt++;
for (int i = 0; i < E; i++){
if (((d[es[i].from] - es[i].tax)>=0)&&d[es[i].to] < (d[es[i].from] - es[i].tax)*es[i].rate){
d[es[i].to] = (d[es[i].from] - es[i].tax)*es[i].rate;
v = 0;
}
}
if (v)return true;
if (cnt == n)return false;
}
}
int main(){
cin >> n >> m >> s >> sum;
for (int i = 0; i < m; i++){
cin >> es[E].from >> es[E].to >> es[E].rate >> es[E].tax;
E++;
es[E].from = es[E - 1].to;
es[E].to = es[E - 1].from;
cin >> es[E].rate >> es[E].tax;
E++;
}
if (!bellman(s))cout << "YES" << endl;
else cout << "NO" << endl;
}