Pagini recente » Cod sursa (job #3003218) | Cod sursa (job #1908580) | Cod sursa (job #762910) | Cod sursa (job #584669) | Cod sursa (job #3255240)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, a, b, dist[30005];
vector<pair<int, int>>v[30005];
queue<int>q;
void bfs(){
while(!q.empty()){
int nod = q.front();
q.pop();
if(nod==b){
fout << dist[nod];
return;
}
for(auto y : v[nod]){
if(dist[y.first]==0){
if(y.first < b){
dist[y.first]=dist[nod]+y.second;
q.push(y.first);
}
else{
dist[y.first]=dist[nod]-y.second;
q.push(y.first);
}
}
}
}
}
int main(){
fin >> n >> m >> a >> b;
for(int i = 1; i <= m; ++i){
int x, y, c;
fin >> x >> y >> c;
v[x].push_back(make_pair(y, c));
v[y].push_back(make_pair(x, c));
}
q.push(a);
bfs();
return 0;
}