单源最短路径(堆优化)dijstra

 倦了


#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1e4+10;
const int maxm=5e5+10;
const int inf=0x3f3f3f3f;
int head[maxn],dis[maxn],vis[maxn],cnt,n,m,s,a,b,c,k,sum;
struct Edge
{
	int before;
	int to;
	int w;
}edge[maxm<<1];
void add(int u,int v,int w)
{
	edge[k].to=v;
	edge[k].w=w;
	edge[k].before=head[u];
	head[u]=k++;
} 
int main()
{
	k=0;
	memset(dis,inf,sizeof dis);
	memset(head,-1,sizeof head);
	memset(vis,0,sizeof vis);
	cin>>n>>m>>s;
	for(int i=0;i<m;i++)
	{
		cin>>a>>b>>c;
		add(a,b,c);
	}
	typedef pair<int,int> dui;
	priority_queue<dui,vector<dui> ,greater<dui> > q; 	
	dis[s]=0;
	q.push(make_pair(0,s));
	while(!q.empty())
	{
		int distance=q.top().first;
		int dot=q.top().second;
		q.pop();
		if(vis[dot])
		continue;
		vis[dot]=1;
		for(int i=head[dot];i!=-1;i=edge[i].before)
		{
			if(dis[edge[i].to]>dis[dot]+edge[i].w)
			{
				dis[edge[i].to]=dis[dot]+edge[i].w;
				q.push(make_pair(dis[edge[i].to],edge[i].to));
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(dis[i]==inf)
		{
			cout<<2147483647<<" ";
			continue;
		}
		cout<<dis[i]<<" ";
	}
//	cout<<endl;
//	int start;
//	cin>>start;
//	for(int i=head[start];i!=-1;i=edge[i].before)
//	cout<<start<<"->"<<edge[i].to<<" "<<edge[i].w<<endl;
	return 0;
}








2020.11.8更新

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
	ios::sync_with_stdio(false); \
	// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 9999999967;
const double pi = acos(-1);
const double eps = 1e-8;
struct Edge
{
	int next, to;
	LL w;
} e[maxn << 1];
struct Node
{
	int dot;
	LL val;
	Node() {}
	Node(int a, LL b)
	{
		dot = a, val = b;
	}
};
int head[maxn];
int n, m, s, k;
LL dis[maxn];
bool vis[maxn];
struct cmp
{
	bool operator()(const Node a, const Node b)
	{
		return a.val > b.val;
	}
};
void add(int u, int v, int w)
{
	e[k].next = head[u];
	e[k].to = v;
	e[k].w = w;
	head[u] = k++;
}
void Dijkstra(int s)
{
	memset(dis, inf, sizeof dis);
	priority_queue<Node, vector<Node>, cmp> q;
	dis[s] = 0;
	q.push(Node(s, 0));
	while (!q.empty())
	{
		Node now = q.top();
		q.pop();
		int u = now.dot;
		if (vis[u] == true)
			continue;
		vis[u] = true;
		for (int i = head[u]; i != -1; i = e[i].next)
		{
			int v = e[i].to;
			if (dis[u] + e[i].w < dis[v])
			{
				dis[v] = dis[u] + e[i].w;
				q.push(Node(v, dis[v]));
			}
		}
	}
	return;
}

int main()
{
#ifdef WXY
	freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
	IO;
	int a, b, c;
	memset(head, -1, sizeof head);
	cin >> n >> m >> s;
	for (int i = 0; i < m; i++)
	{
		cin >> a >> b >> c;
		add(a, b, c);
	}
	Dijkstra(s);
	for (int i = 1; i <= n; i++)
		cout << dis[i] << " ";
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值