带权值的刘汝佳最短路

#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<cstdio>
#include<queue>
#define ll long long
const ll MAXN = 1e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;
using namespace std;
struct Edge{
	ll from;ll to;ll dist,cost;
	Edge(){}
	Edge(ll from,ll to,ll dist,ll cost) : from(from),to(to),dist(dist),cost(cost){}
};
struct djs{
	ll n,m;
	vector<Edge>edges;
	vector<ll>G[MAXN];
	
	ll vis[MAXN];
	ll d[MAXN];
	ll c[MAXN];
	void init(ll n)
	{
		this->n = n;m = 0;
		edges.clear();
		for(ll i = 0; i <= n ;++i) G[i].clear();
	}
	void add(ll from,ll to,ll dist,ll cost)
	{
		edges.push_back(Edge(from,to,dist,cost));
		m = edges.size();
		G[from].push_back(m - 1);
	}
	struct HeapNode
	{
		ll u,d,c;
		bool operator < (const HeapNode& aa) const
		{
			if(aa.d == d)
			{
				return aa.c < c;
			}
			return aa.d < d;
		}	
		HeapNode(ll from,ll w,ll c):u(from),d(w),c(c){
		}
	};
	void djst(ll s)
	{
		priority_queue<HeapNode>Q;
		for(ll i = 0; i <= n ;++i) d[i] = inf,c[i] = inf;
		d[s] = 0;c[s] = 0;
		memset(vis,0,sizeof(vis));
		Q.push(HeapNode(s,0,0));
		while(!Q.empty())
		{
			HeapNode x = Q.top();
			Q.pop();
			ll u =x.u;
			if(vis[u])continue;
			vis[u] = true;
			for(ll i = 0;i < G[u].size();++i)
			{
				Edge& e = edges[G[u][i]];
				if(d[e.to] > d[u] + e.dist)
				{
					d[e.to] = d[u] + e.dist;
					c[e.to] = e.cost;
					Q.push(HeapNode(e.to,d[e.to],c[e.to]));
				}
				else if(d[e.to] == d[u] +e.dist)
				{
					c[e.to] = min(c[e.to] , e.cost ) ; 
					Q.push(HeapNode(e.to,d[e.to] ,c[e.to]));
				}
			}
		}
	}
}gao;
发布了219 篇原创文章 · 获赞 43 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_43568078/article/details/89461508
今日推荐