#include<iostream>
#include<algorithm>
#include<list>
#include<string.h>
#include<math.h>
#include<vector>
#include<stdio.h>
using namespace std;
#define N 1005
const int Inf =999999;
int n,m,s;
double t;
//货币种类数;货币点数目;起始货币;货币数目
struct node{
int to;
double r;
double c;
};
vector<node> g[N];
bool spfa(){
double d[N]={0};
d[s] = t;
list<int>ls;
ls.push_back(s);
int isLs[N]={0};//记录是否已经在队列中
int cou[N]={0}; //记录加入次数
isLs[s] = 1;
cou[s] = 1;
while(!ls.empty()) {
int l = ls.front();//每次从队列中取出一个点
ls.pop_front();
isLs[l] =0;
for(int i=0;i<g[l].size();i++){
int to = g[l][i].to;
double r = g[l][i].r;
double c = g[l][i].c;
if(d[to]<(d[l]-c)*r){//如果以该点作为中介能够降低其他点的最短路径,也就是松弛操作
d[to] = (d[l]-c)*r;//更新其他点的最短距离
ls.push_back(to); //将其他点加入队列
isLs[to] = 1;
cou[to]++;
if(cou[to]>n){ //如果存在点进入队列大于n次,说明存在环
return false;
}
}
}
}
return true;
}
int main()
{
cin>>n>>m>>s>>t;
for(int i=0;i<m;i++){
struct node edge1,edge2;
int k1,k2;
double r12,c12,r21,c21;
cin>>k1>>k2>>r12>>c12>>r21>>c21;
edge1.to = k2;
edge1.r = r12;
edge1.c = c12;
g[k1].push_back(edge1);
edge2.to = k1;
edge2.r = r21;
edge2.c = c21;
g[k2].push_back(edge2);
}
bool res= spfa();
if(res){
cout<<"NO"<<endl;
}else{
cout<<"YES"<<endl;
}
return 0;
}
SPFA
最新推荐文章于 2023-12-25 22:46:13 发布