Home Article Practice c++ spfa模板代码

c++ spfa模板代码

2022-05-25 15:21  views:1298  source:小高高123    

#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> > g[1000];
int n,m;
int d[1000],cnt[1000];
bool inq[1000];
int INF=1e9;
bool fail;
void spfa(int start){
fill(d,d+n,INF);
fill(inq,inq+n,false);
fill(cnt,cnt+n,INF);
fail=false;
d[start]=0;
cnt[start]=0;
inq[start]=true;
queue<int> q;
q.push(start);
while(!fail&&!q.empty()){
int dot=q.front();
q.pop();
inq[dot]=false;
for(int i=0;i<g[dot].size()&&!fail;i++){
int to=g[dot][i].first,weight=g[dot][i].second;
if(d[dot]+weight<d[to]){
d[to]=d[dot]+weight;
cnt[to]=cnt[dot]+1;
if(cnt[to]>n){
fail=true;
break;
}
if(!inq[to]){
inq[to]=true;
q.push(to);
}
}
}
}
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)