#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N=2510,M = 6200 * 2 + 10;
int h[N];int dist[N];int e[M];int ne[M];int w[M];int idx;
int n,m,S,T;
bool st[N];
void add(int a,int b,int c){
e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
void spfa(){
memset(dist,0x3f,sizeof dist);
queue<int> q;
dist[S]=0;
st[S]=true;
q.push(S);
while(!q.empty()){
int t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i]){
int j=e[i];
if(dist[t]+w[i]<dist[j]){
dist[j]=dist[t]+w[i];
if(!st[j]){
st[j]=true;
q.push(j);
}
}
}
}
}
int main(){
cin >> n >> m >> S >> T;
memset(h,-1,sizeof h);
for(int i=0;i<m;i++){
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
add(b,a,c);
}
spfa();
cout << dist[T];
}
spfa模板
最新推荐文章于 2023-06-18 13:52:14 发布