SPFA模板(vector版本)

入门参考https://www.cnblogs.com/bofengyu/p/5004398.html。

#pragma comment(linker, "/STACK:102400000,102400000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define pt(a) cout<=a;i--)
#define pii pair
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
const ll mod = 1e9+7;
const int N = 1e6+10;

struct Edge{
    int v,cost;
};
vectorE[N];

bool vis[N];///在队列标志
int cnt[N];///每个点的入队列次数
int d[N];

bool spfa(int s,int n){///判断是否有负环
    mst(vis,false);
    mst(d,0);
    vis[s]=true;
    d[s]=0;
    queueq;
    q.push(s);
    mst(cnt,0);
    cnt[s]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=0; id[u]+E[u][i].cost){
                d[v]=d[u]+E[u][i].cost;
                if(!vis[v]){
                    vis[v]=true;
                    q.push(v);
                    if(++cnt[v]>n)return false;//cnt[i] 为入队列次数,用来判定是否存在负环回路
                }
            }
        }
    }
    return true;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    return 0;
}

 

你可能感兴趣的:(SPFA,最短路,图论)