spfa 模板

来源:互联网 发布:用别人身份证开淘宝店 编辑:程序博客网 时间:2024/06/07 02:00

只是一个没有main的模板

#include <bits/stdc++.h>#define constant const constexprtypedef long long ll;typedef long double db;typedef std::string str;constant int maxN = 1e5 + 3;int N=5e6,M,P,Q,T;constant int iinf = 2147483647;constant ll linf = 9223372036854775807LL;constant ll mod = 1e9 + 7;constant db pi = std::acos(-1.0);constant db eps = 1e-8;typedef int intvec[maxN];typedef bool boolvec[maxN];typedef ll llvec[maxN];typedef db dbvec[maxN];typedef str strvec[maxN];typedef char c_str[maxN];template <typename T> inline constant T abs(T x) {return x>=0?x:-x;}inline db log2(db x) {return std::log(x)/std::log(2);}void quicker() {std::ios_base::sync_with_stdio(false);std::cin.tie(nullptr);}template <typename T> inline int sgn(T x){return x>eps?1:(x<-eps?-1:0);}intvec head,to,next;llvec weight;int edge_count;void init(){std::fill_n(head,maxN,-1);std::fill_n(to,maxN,-1);std::fill_n(next,maxN,-1);std::fill_n(weight,maxN,0LL);edge_count = 0;}void add(int u,int v,ll w){to[edge_count] = v;next[edge_count] = head[u];weight[edge_count] = w;head[u] = edge_count++;}void biadd(int u,int v,ll w){add(u,v,w);add(v,u,w);}llvec dist;intvec parent;boolvec in;std::stack<int> q;void spfa_init(){std::fill_n(dist,maxN,linf);std::fill_n(parent,maxN,-1);std::fill_n(in,maxN,false);while (!q.empty()) q.pop();}ll spfa(int u,int v){dist[u] = 0LL;q.emplace(u);in[u] = true;while (!q.empty()){int t = q.top();q.pop();in[t] = false;for (int i=head[t];i>=0;i=next[i]){int s = to[i];if (dist[s] > dist[t] + weight[i]){dist[s] = dist[t] + weight[i];parent[s] = t;if (!(in[s])){q.emplace(s);in[s] = true;}}}}return dist[v];}


原创粉丝点击