提示:关键在于反向利用Bellman-Ford算法
题目大意
有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加
货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的
怎么找正权回路呢?(正权回路:在这一回路上,顶点的权值能不断增加即能一直进行松弛)
题目分析:
一种货币就是图上的一个点
一个“兑换点”就是图上两种货币之间的一个兑换环,相当于“兑换方式”M的个数,是双边
唯一值得注意的是权值,当拥有货币A的数量为V时,A到A的权值为K,即没有兑换
而A到B的权值为(V-Cab)*Rab
本题是“求最大路径”,之所以被归类为“求最小路径”是因为本题题恰恰与bellman-Ford算法的松弛条件相反,求的是能无限松弛的最大正权路径,但是依然能够利用bellman-Ford的思想去解题。
因此初始化d(S)=V 而源点到其他店的距离(权值)初始化为无穷小(0),当s到其他某点的距离能不断变大时,说明存在最大路径
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <queue>
#include <map>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-4
#define maxn 100010
#define MOD 1000000007
int n; //货币种数
int m; //兑换点数量
int s; //持有第s种货币
double v; //持有的s货币的本金
int all; //边总数
double dis[101]; //s到各点的权值
class exchange_points
{
public:
int a; //货币a
int b; //货币b
double r; //rate
double c; //手续费
}exc[202];
int bellman()
{
memset(dis,0,sizeof(dis));
dis[s] = v;
int flag;
for(int i = 1; i < n; i++)
{
flag = 0;
for(int j = 0; j < all; j++)
if(dis[exc[j].b] < (dis[exc[j].a]-exc[j].c) * exc[j].r)
{
dis[exc[j].b] = (dis[exc[j].a]-exc[j].c) * exc[j].r;
flag = 1;
}
if(!flag)
break;
}
for(int i = 0; i < all; i++)
{
if(dis[exc[i].b] < (dis[exc[i].a]-exc[i].c)*exc[i].r)
return 1;
}
return 0;
}
int main()
{
while(scanf("%d%d%d%lf",&n,&m,&s,&v) != EOF)
{
all = 0;
int a,b;
double r1,r2,c1,c2;
for(int i = 0; i < m; i++)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
exc[all].a = a;
exc[all].b = b;
exc[all].r = r1;
exc[all++].c = c1;
exc[all].a = b;
exc[all].b = a;
exc[all].r = r2;
exc[all++].c = c2;
}
if(bellman())
printf("YES\n");
else
printf("NO\n");
}
}