首页 练字文章 c++ spfa模板代码

c++ spfa模板代码

2022-05-25 15:21  浏览数:997  来源:小高高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);
}
}
}
}
}



声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。

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