dijkstra算法(堆优化)

#include 
using namespace std;
#define ll long long
typedef struct node
{
	ll to,val;
}node;
vector side[100005];
bool sign[100005];
ll distan[100005];
struct cmp{
	bool operator()(node a,node b)
	{
		return a.val>b.val;
	}
};
int main()
{
	ll n,m,s;scanf("%lld%lld%lld",&n,&m,&s);//节点数,边数,源点 
	for(int i=0;i<=n;i++) distan[i]=LLONG_MAX/2,sign[i]=0,side[i].clear();//初始化 
	node tem;
	for(int i=0;i,cmp> p_que;
	for(int i=1;i<=n;i++)
	{
		tem.to=i,tem.val=distan[i];
	    p_que.push(tem);
	}
	
	while(!p_que.empty())
	{
		node p=p_que.top();
		int u=p.to;
		p_que.pop();
		if(sign[u]) continue;
		sign[u]=1;
		for(int i=0;idistan[u]+side[u][i].val)
			{
				distan[v]=min(distan[v],distan[u]+side[u][i].val);
			    tem.to=v,tem.val=distan[v];
			    p_que.push(tem);
			}
		}
	}
	
	for(int i=1;i<=n;i++) printf("%lld ",distan[i]);
	return 0;
}

你可能感兴趣的:(一些模板,算法)