#include "stdafx.h"
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=100;
const int inf=0x7fffffff;
struct node{
int v,w;
};
vector<node>G[maxn];
int d[maxn],n,num[maxn]={0};//记录顶点的入队次数
bool inq[maxn]={0};//记录是否在队列中
bool spfa(int s){
fill(d,d+maxn,inf);
queue<int>q;
q.push(s);
d[s]=0;
inq[s]=true;
num[s]++;
while(!q.empty()){
int u=q.front();
q.pop();
inq[u]=false;
for(int i=0;i<G[f].size();i++){
int v=G[f][i].v;
int w=G[f][i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(inq[v]==0){
q.push(v);
inq[v]=true;
num[v]++;
if(num[v]>=n)return false;
}
}
}
}
return true;
}
SPFA算法判断是否有负环
最新推荐文章于 2025-01-30 19:43:59 发布