版权声明:听说这里让写版权声明~~~ https://blog.csdn.net/m0_37691414/article/details/82054152
解析:判断是否存在回路。
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<cstdio> using namespace std; const int maxn = 110; struct node{ int to; double r, c; }; double dis[maxn]; int cnt[maxn]; bool visit[maxn]; vector<node> e[maxn]; int n, m, s; double v; bool solve(){ queue<int> q; q.push(s); dis[s] = v; visit[s] = true; cnt[s]++; while(!q.empty()){ int from = q.front(); q.pop(); visit[from] = false; for(int i = 0; i < e[from].size(); ++i){ node &P = e[from][i]; int to = P.to; double r = P.r, c = P.c; if(dis[to] < (dis[from] - c)*r){ dis[to] = (dis[from] - c)*r; if(visit[to] == false) visit[to] = true, q.push(to), cnt[to]++; if(cnt[to] > n) return true; } } } return false; } int main(){ scanf("%d%d%d%lf", &n, &m, &s, &v); int from, to; double r, c; for(int i = 0; i < m; ++i){ scanf("%d%d%lf%lf", &from, &to, &r, &c); e[from].push_back((node){to, r, c}); scanf("%lf%lf", &r, &c); e[to].push_back((node){from, r, c}); } if(solve()) printf("YES\n"); else printf("NO\n"); return 0; }