朴素dijkstra 90pts 求调!!最后一个点RE了
  • 板块P1396 营救
  • 楼主lgj3475
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/2/23 18:00
  • 上次更新2024/2/23 20:42:47
查看原帖
朴素dijkstra 90pts 求调!!最后一个点RE了
1062172
lgj3475楼主2024/2/23 18:00
include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>

using namespace std;
typedef pair<int, int> PII;
const int N = 1e4 + 10, M = 2e4 + 10;

int h[N], e[N], ne[N], w[N], idx;

int dis[N];//存第i个点到起点的最小拥挤度
bool st[N];//标记已经确定了最短拥挤度的点

int n, m, s, ed;

void insert(int a, int b, int c)
{
	e[idx] = b;
	w[idx] = c;
	ne[idx] = h[a];
	h[a] = idx++;
}

void dijkstra()
{
	memset(dis, 0x3f, sizeof dis);
	dis[s] = 0;
	priority_queue<PII, vector<PII>, greater<PII>> heap;		 //声明小根堆
	heap.push({ 0,s });		//起点入堆

	while (heap.size())
	{
		auto t = heap.top();
		heap.pop();
		int p = t.second,  d = t.first;

		if (st[p]) continue;//防止重复更新同一条路
		st[p] = true;
      //标记这条路已经更新过,后面如果走过相同的路一定比现在这条路挤,不再更新

		for (int i = h[p]; i != -1; i = ne[i])//开始更新p可以到达的道
		{
			int j = e[i];
			int d1 = w[i];
			if (dis[j] > max(d,d1))
			{
				dis[j] = max(d,d1);
				heap.push({ dis[j],j });
			}
		}
		
	}
	cout << dis[ed];
}

int main()
{
	cin >> n >> m >> s >> ed;
	
  //用邻接表存双向图,先初始化,-1标记末端
	memset(h, -1, sizeof h);

	while (m--)//插入m条边
	{
		int a, b, c;
		cin >> a >> b >> c;
		insert(a, b, c);
		insert(b, a, c);
	}
	//运行dijkatra算法
	dijkstra();

	return 0;
}

2024/2/23 18:00
加载中...