洛谷 P3771 (SPFA 图邻接表)

#include 
#define int long long 

using namespace std;
typedef pair ii;
typedef vector vii;
typedef vector vi;
const int INF=2147483647;

int32_t main(){
	// interesting
	int n,m,s;cin>>n>>m>>s;
	s-=1;
	vector> > gra(n);
	gra.assign(n, vii());
	for(int i=0;i>u>>v>>w;
		u-=1;v-=1;
		gra[u].push_back(ii(v,w));
	}
	queue q;
	vector in_queue(n,0);
	vector dist(n,INF);
	q.push(s);
	dist[s]=0;
	in_queue[s]=1;
	while(!q.empty()){
		int u=q.front();q.pop();
		in_queue[u]=0;
		for(int i=0;i<(int)gra[u].size();i++){
			int v=gra[u][i].first;
			int wei=gra[u][i].second;
			if(dist[u]+wei< dist[v]){
				dist[v]=dist[u]+wei;
				if(!in_queue[v]){
					q.push(v);
					in_queue[v]=1;
				}
			}
		}
	}
	
	for(int i=0;i

 

你可能感兴趣的:(洛谷,最短路)