spfa 维护起点到每个点的最短路经过了几个边。
如果大于等于n肯定存在负环(否则最多n-1)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
int vs[M];
ll d[M];
int nm[M];
int n,m;
bool spfa()
{
memset(d,0x3f,sizeof(d));
memset(nm,0,sizeof(nm));
memset(vs,0,sizeof(vs));
queue<int>q;
q.push(1);
d[1]=0;vs[1]=1;
while(q.size())
{
int x=q.front();q.pop();
vs[x]=0;
for(int i=head[x];i;i=ee[i].nxt)
{
int y=ee[i].to,w=ee[i].w;
if(d[y]>d[x]+w)
{
d[y]=d[x]+w;
nm[y]=nm[x]+1;//到y点的最短路,需要的路径数
// cout<<