spfa 模板

只是一个没有main的模板

#include <bits/stdc++.h>
#define constant const constexpr
typedef 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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值