Qusetion Link
Problem analysising
There is n type money, just like the node, m curreny exchange points ,just like m edges.
The first thing you should know is that the digraph is directed.The cost from u node to v node is (100 - 0.39)×29.75=2963.3975. You should do is trying to find a way to make money become more and more.Try to think Bellman-Ford algorithm is how to find the negative edge.The negative edge is make the cost of starting point to ending point less and less,so we can make arry called dis different , chang something ,then we can find a way make the money arise.
This is AC code
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val) memset(var,val,sizeof(var))
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
while ( a>='0' && a<='9' ){ x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 109
int n,m,s;double v;
struct node{
int u,v;
double h,y;
}e[N*N];int tot = 0;double dis[N];
void add_ead(int u,int v,double uvh,double uvy){
++tot;
e[tot].u = u; e[tot].v = v; e[tot].h = uvh; e[tot].y = uvy;
}
bool bellman(){
rep(i,1,n) dis[i] = 0;
dis[s] = v;
for (int ai = 1 ; ai <= n; ai++){
for (int bi = 1; bi <= tot; bi++){
int u = e[bi].u , v = e[bi].v ; double h = e[bi].h , y = e[bi].y;
if ( dis[v] < ( dis[u] - y )*h ){
dis[v] = (dis[u] - y )*h;
}
}
}
rep(i,1,tot)
if ( dis[e[i].v] < (dis[e[i].u]-e[i].y)*e[i].h ) return true;
return false;
}
int main()
{
//#ifdef LOCAL
// freopen("1.txt","r",stdin);
//#endif
srand((int)time(0));
// std::ios::sync_with_stdio(false);
scanf("%d%d%d%lf",&n,&m,&s,&v);
rep(i,1,m){
int u,v; double uvh,uvy,vuh,vuy;
scanf("%d%d%lf%lf%lf%lf",&u,&v,&uvh,&uvy,&vuh,&vuy);
add_ead(u,v,uvh,uvy);
add_ead(v,u,vuh,vuy);
}
if ( bellman() ) printf("YES\n");
else printf("NO\n");
return 0;
}