Pagini recente » Cod sursa (job #347562) | Cod sursa (job #2943241) | Cod sursa (job #2651682) | Borderou de evaluare (job #212912) | Cod sursa (job #2965775)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("sate.in");
ofstream cout("sate.out");
const int NMAX = 1e5;
const int inf = 1e9;
queue <int> q;
vector <pair <int, int>> g[NMAX + 1];
int d[NMAX + 1];
void bfs(){
while(!q.empty()){
int x = q.front();
q.pop();
for(auto y : g[x]){
if(d[y.first] > d[x] + y.second){
d[y.first] = y.second + d[x];
q.push(y.first);
}
}
}
}
int main(){
ios :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n = 0, m = 0, x1 = 0, x2 = 0;
cin >> n >> m >> x1 >> x2;
for(int i = 0; i < m; i++){
int x = 0, y = 0, d = 0;
cin >> x >> y >> d;
g[x].push_back({y, d});
g[y].push_back({x, -d});
}
for(int i = 1; i <= n; i++)
d[i] = inf;
d[x1] = 0;
q.push(x1);
bfs();
cout << d[x2];
}