Pagini recente » Cod sursa (job #3245559) | Cod sursa (job #2609792) | Cod sursa (job #2066220) | Cod sursa (job #949736) | Cod sursa (job #3255243)
#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]-1;
return ;
}
for(auto y : v[nod]){
if(dist[y.first]==0){
if(y.first < nod){
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));
}
dist[a] = 1;
q.push(a);
bfs();
return 0;
}