Pagini recente » Cod sursa (job #1971193) | Cod sursa (job #386655) | Cod sursa (job #3210373) | Cod sursa (job #2818509) | Cod sursa (job #3268204)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int NMAX = 30005;
vector <pair <int,int>> a[NMAX];
vector <int> dist(NMAX);
bitset <NMAX> viz;
queue <int> q;
int n, m, x, y, c, s, f;
void bfs(){
int v;
while(!q.empty()){
v=q.front();
q.pop();
viz[v]=1;
for(auto next:a[v]){
if(!viz[next.first]){
if(next.first<v){
dist[next.first]=dist[v]-next.second;
q.push(next.first);
}
else{
dist[next.first]=dist[v]+next.second;
q.push(next.first);
}
}
}
}
}
int main(){
fin>>n>>m>>s>>f;
for(int i=1;i<=m;i++){
fin>>x>>y>>c;
a[x].push_back({y,c});
a[y].push_back({x,c});
}
dist[s]=0;
q.push(s);
bfs();
fout<<dist[f];
return 0;
}