题意:有多种货币,货币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加
思路:把货币当作顶点,不同货币之间汇率和手续费作为边的信息建图,只要找出是否存在正权回路,且最后得到的s金额是增加的即可,在bellman-ford求负环是将d[i]初始化为无穷大,这里我们初始化为0即可
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100000
#define LL long long
int cas=1,T;
struct Edge
{
int from,to;
double rab,cab;//,rba,cba;
Edge(int u,int v,double ra,double ca):from(u),to(v),rab(ra),cab(ca){}
};
double has = 0;
int n,m,num;
struct Bellford
{
vector<Edge> edges;
vector<int> G[200];
bool inq[205];
double d[205];
int cnt[205];
void init(int n)
{
for (int i = 0;i<=n;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,double ra,double ca)
{
int mm;
edges.push_back(Edge(from,to,ra,ca));
mm = edges.size();
G[from].push_back(mm-1);
}
bool bellman_ford(int s)
{
// printf("1");
queue<double> q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
for (int i = 0;i<=n;i++)
d[i]=0;
d[s]=has;
inq[s]=1;
q.push(s);
// int flag = 0;
while (!q.empty())
{
// printf("1\n");
int u = q.front();q.pop();
inq[u]=0;
for (int i = 0;i<G[u].size();i++)
{
Edge &e = edges[G[u][i]];
/* if (d[s]>20)
{
flag = 1;
break;
}*/
if (d[e.to] < (d[e.from]-e.cab) * e.rab)
{
d[e.to] = (d[e.from]-e.cab) * e.rab;
if (!inq[e.to])
{
q.push(e.to);
inq[e.to]=1;
if (++cnt[e.to] > n)
return false;
}
}
}
}
return true;
}
};
int main()
{
while (scanf("%d%d%d%lf",&n,&m,&num,&has)!=EOF)
{
Bellford bf;
bf.init(n);
for (int i = 1;i<=m;i++)
{
int a,b;
double c,d,e,f;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
bf.AddEdge(a,b,c,d);
bf.AddEdge(b,a,e,f);
}
int flag = bf.bellman_ford(num);
if (!flag || bf.d[num] > has)
puts("YES");
else
puts("NO");
}
//freopen("in","r",stdin);
//scanf("%d",&T);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}