SPFA
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int n,m,s,v;double w;
struct node
{
int to;
double val,del;
};
vector<node>G[105];
queue<int>que;
int spfa(int s)
{
int vis[105];double d[105];
memset(d,0,sizeof d);
memset(vis,0,sizeof vis);
que.push(s);
d[s]=w;
vis[s]=1;
while(que.size())
{
int u=que.front();
que.pop();
vis[u]=0;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].to;
double p=G[u][i].val;
double q=G[u][i].del;
if(d[v]<((d[u]-q)*p))
{
d[v]=(d[u]-q)*p;
if(vis[v]==0)
{
que.push(v);
vis[v]=1;
}
}
if(d[s]>w)
return 1;
}
}
return 0;
}
int main()
{
scanf("%d%d%d%lf",&n,&m,&s,&w);
while(m--)
{
int u,v;double p,q;
scanf("%d%d%lf%lf",&u,&v,&p,&q);
G[u].push_back((node){v,p,q});
scanf("%lf%lf",&p,&q);
G[v].push_back((node){u,p,q});
}
if(spfa(s)) puts("YES");
else puts("NO");
return 0;
}
Flody
#include<cstdio>
int n,m,s;
double v,dis[105],e1[105][105],e2[105][105];
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dis[j]<(dis[i]-e2[i][j])*e1[i][j])
dis[j]=(dis[i]-e2[i][j])*e1[i][j];
}
int judge()
{
double e[105];
for(int i=1;i<=n;i++) e[i]=dis[i];
floyd();
for(int i=1;i<=n;i++) if(e[i]<dis[i]) return 1;
return 0;
}
int main()
{
scanf("%d%d%d%lf",&n,&m,&s,&v);
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);
e1[a][b]=c;e1[b][a]=e;
e2[a][b]=d;e2[b][a]=f;
}
dis[s]=v;
floyd();
if(judge()) puts("YES");
else puts("NO");
return 0;
}