码迷,mamicode.com
首页 > 其他好文 > 详细

spfa

时间:2019-01-19 13:23:49      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:return   get   sha   air   void   second   printf   spfa   make   

关于spfa它已经死了

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5,maxm = 1e6+5,inf = 1e9;
priority_queue < pair<int,int> >q;
int d[maxn],p[maxn],begin[maxn],to[maxm],next[maxm],w[maxm];
int n,m,e,s;
inline int read(){
	int x = 0,k = 1;char ch = getchar();
	while(ch < ‘0‘ || ch > ‘9‘){if(ch == ‘-‘)k = -1;ch = getchar();}
	while(ch >= ‘0‘ && ch <= ‘9‘)x = (x<<3)+(x<<1)+(ch^48),ch = getchar();
	return x*k;
}
inline void add(int x,int y,int z){
	next[++e] = begin[x];
	begin[x] = e;
	to[e] = y;
	w[e] = z;
}
inline void init(){
	n = read();
	m = read();
	s = read();
	for(int i = 1,x,y,z;i <= m;i++){
		x = read();
		y = read();
		z = read();
		add(x,y,z);
	}
}
inline void out(){
	for(int i = 1;i <= n;i++)
		printf("%d%c",d[i] == inf ? 2147483647 : d[i],i == n ? ‘\n‘ : ‘ ‘);
}
inline void spfa(){
	for(int i = 1;i <= n;i++)d[i] = inf;
	d[s] = 0;
	q.push(make_pair(0,s));
	while(!q.empty()){
		int tmp = q.top().second;
		q.pop();
		if(p[tmp])continue;
		p[tmp] = 1;
		for(int i = begin[tmp];i;i = next[i])
			if(d[to[i]] > d[tmp]+w[i])
				d[to[i]] = d[tmp]+w[i],q.push(make_pair(-d[to[i]],to[i]));
	}
}
int main(){
	init();
	spfa();
	out();
	return 0;
}

  

spfa

标签:return   get   sha   air   void   second   printf   spfa   make   

原文地址:https://www.cnblogs.com/wangyifan124/p/10291289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!