n种货币,m对货币可以互相兑换,不过有汇率和手续费,问是否能够以某种方式使当前持有的货币增加
需要回到起始点后钱数增加,用spfa松弛 ,存在正环且能到达则说明能够无限增加钱数,等到钱数增加到很大再返回来即可,故只要判断是否存在正环
//需要回到起始点后钱数增加,spfa松弛 ,存在正环且能到达则说明能够无限增加钱数,等到钱数增加到很大再返回来即可,故只要判断是否存在正环
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int> PII;
const int MAXN=111;
const int MAXM=MAXN*MAXN/2;
const int INF=0x3f3f3f3f;
int head[MAXN],to[MAXM<<1],ne[MAXM<<1],n,m,ecnt,T;
double dist[MAXN],wt1[MAXM<<1],wt2[MAXM<<1],val;
void init()
{
ecnt=0;
memset(head,0,sizeof(head));
}
void addedge(int a,int b,double c,double d)
{
ne[++ecnt]=head[a];
head[a]=ecnt;
to[ecnt]=b;
wt1[ecnt]=c;
wt2[ecnt]=d;
}
queue<int> q;
int inq[MAXN],vis[MAXN];
bool spfa(int s)
{
while(!q.empty())
q.pop();
for(int i=1;i<=n;i++)
dist[i]=0,inq[i]=0,vis[i]=0;
dist[s]=val;
inq[s]=1;
q.push(s);
double ta;
while(!q.empty())
{
int fr=q.front();
q.pop();
inq[fr]=0;
for(int i=head[fr];i;i=ne[i])
{
int v=to[i];
if(dist[fr]<wt2[i])
continue;
ta=(dist[fr]-wt2[i])*wt1[i];
if(dist[v]<ta)
{
if(v==s)
return true;
dist[v]=ta;
if(inq[v])continue;
if(++vis[v]>=n)
return true;
inq[v]=1;
q.push(v);
}
}
}
return false;
}
int main()
{
int st,ta,tb;
double tc,td,te,tf;
scanf("%d%d%d%lf",&n,&m,&st,&val);
init();
for(int i=1;i<=m;i++)
{
scanf("%d%d%lf%lf%lf%lf",&ta,&tb,&tc,&td,&te,&tf);
addedge(ta,tb,tc,td);
addedge(tb,ta,te,tf);
}
printf("%s\n",spfa(st)?"YES":"NO");
return 0;
}