用map实现的邻接矩阵,二叉堆优化,求助
查看原帖
用map实现的邻接矩阵,二叉堆优化,求助
159833
码迷元首楼主2022/9/17 23:11
#include<bits/stdc++.h>
#define inf 1000000001

struct side
{
	int to;
	int w;
	bool operator <(side b) const
	{
		return w < b.w;
	}
};
struct path
{
	int u,v;
	bool operator<(path b)const
	{
		return u+v<b.u+b.v;
	}
};
int n, m, s;
int dis[100001];
std::priority_queue<side> h;
std::map<path,int> graph;
int main()
{
	scanf("%d%d%d", &n, &m, &s);
	for (int i = 1; i <= m; i++)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		graph[path{u,v}] = w;
	}
	for (int i = 1; i <= n; i++)
		dis[i] = inf;
	
	dis[s] = 0;
	h.push(side{s, 0});
	while (!h.empty())
	{
		side tmp = h.top();
		h.pop();
		int x = tmp.to;
		if (dis[x] != tmp.w)continue;
		for (int i = 1; i <= n; i++)
		{
			if (graph.count(path{x,i})==0)continue;
			
			int y = i, w =graph[path{x,i}];
			if (dis[y] > dis[x] + w)
			{
				dis[y] = dis[x] + w;
				h.push(side{y, dis[y]});
			}
		}
	}
	for (int i = 1; i <= n; i++)printf("%d ", dis[i]);
	
}
2022/9/17 23:11
加载中...