プライオリティキュー最適化ダイクストラ(最初にすべての繰り返し)

ahhhhhhhhhhh、ブロガーは、分割しており、睡眠のない夜のほとんどは、良いことのアソシエイトではありません

 ダイクストラあなたは小さなヒープのトップを使用できるように最小電流更新distのを取る必要があるたびに、distの配列の値を維持し、最短経路を求める私はテレビを見て退屈し、について書いてみてください、WA、変更一見、WA、その後、変更、WA、何も間違ってああ、もう一度WAを試してみてください、順番に注意してくださいは、ヒープの小さな値distのトップに維持されるべきではなく、レコードを何点は、構造体を使用することが可能となります。

Iが配置点(1,2 ,,)の大きさに応じて、優先キューにポイントに直接私を発見し、見て、見え変化、変更、それがGGに確かです。

私は私が愚かな当時の書いたコードQAQを記録し、特別な分類を開くつもりです

コード(右)を取り付け:

/**
 * Author : correct
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#define mem(a, b) memset(a, b, sizeof a)
using namespace std;
const int M = 6210 * 2, N = 2520;
int head[M], nex[M], to[M], cnt, ed[M];
void add(int a, int b, int c){
	to[++cnt] = b;
	nex[cnt] = head[a];
	head[a] = cnt;
	ed[cnt] = c;
}
int n, m, s, t;
int dist[N];
bool vis[N];
struct p{
	int x, dist;
	bool operator < (const p& a)const{
		return this->dist < a.dist;
	}
	bool operator > (const p& b)const{
		return this->dist > b.dist;
	}
	p(){

	}
	p(int x, int dist){
		this->x = x;
		this->dist = dist;
	}
};
priority_queue<p, vector<p>, greater<p> > q;
int main()
{
	mem(head, -1);
	mem(nex, -1);
	cnt = 0;
	mem(dist, 0x3f);
	mem(vis, 0);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> s >> t;
	dist[s] = 0;
	q.push(p(s, 0));
	while (m--){
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	while (q.size()){
		p T = q.top();
		int ind = T.x;
		q.pop();
		if (vis[ind]){
			continue;
		}
		vis[ind] = 1;
		for (int i = head[ind]; ~i; i = nex[i]){
			int y = to[i];
			int c = ed[i];
			if (dist[y] > dist[ind] + c){
				dist[y] = dist[ind] + c;
				q.push(p(y, dist[y]));
			}
		}
	}
	cout << dist[t];
	return 0;
}

 

公開された204元の記事 ウォン称賛13 ビュー10000 +

おすすめ

転載: blog.csdn.net/weixin_43701790/article/details/104831693