Pagini recente » Cod sursa (job #1978108) | Cod sursa (job #3268653) | Cod sursa (job #609582) | Cod sursa (job #1108877) | Cod sursa (job #3275534)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int main(){
ifstream in("sate.in");
ofstream out("sate.out");
int n, m, x0, y0;
in >> n >> m >> x0 >> y0;
vector <pair<int, int>> S[n+1];
for(int i = 0; i < m; i++){
int x, y, d;
in>>x>>y>>d;
S[x].push_back({y, d});
S[y].push_back({x, d});
}
vector <int> d(n+1, 0);
vector <int> vizitat(n+1, 0);
queue<int> q;
q.push(x0);
int x;
d[x]=0;
vizitat[x]=1;
while(!q.empty()){
x = q.front();
q.pop();
for(auto y : S[x]){
if(vizitat[y.first]==0){
vizitat[y.first]=1;
if(y.first > x){
d[y.first] = d[x] + y.second;
}else{
d[y.first] = d[x] - y.second;
}
q.push(y.first);
}
}
}
out<<d[y0];
in.close();
out.close();
return 0;
}