为什么不能用二叉堆优化?
查看原帖
为什么不能用二叉堆优化?
1046223
a_blue_fool楼主2024/4/22 14:10
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>

using namespace std;

int n , m , ans[2000] , u , v , w;

struct node1
{
	int v , w;
};

struct node2
{
	int x , d;
	
	bool operator < (const node2 b) const
	{
		return d < b.d;
	}
}now;

priority_queue <node2> q;
vector <node1> grath[2000];

int main()
{
	memset (ans , -1 , sizeof (ans));
	
	scanf("%d%d" , &n , &m);
	for (int i = 1 ; i <= m ; i++)
	{
		scanf("%d%d%d" , &u , &v , &w);
		grath[u].push_back(node1{v,w});
	}
	
	q.push(node2{1,0});
	
	while (!q.empty())
	{	
		now = q.top();
		q.pop();
		
		if (ans[now.x] != -1) continue;
		
		ans[now.x] = now.d;
		
		for (auto i : grath[now.x])
			q.push(node2{i.v , now.d + i.w});
	}
	
	printf("%d" , ans[n]);
	
	return 0;
}

这是优先队列的

看起来没有任何问题,但是过不了,可是只要换成了一般的队列,就直接过了

2024/4/22 14:10
加载中...