Pagini recente » Cod sursa (job #3304338) | Cod sursa (job #3335730) | Cod sursa (job #841666) | Cod sursa (job #3302953) | Cod sursa (job #3335108)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, s, f;
vector < pair<int,int> > g[30005];
int viz[30005], dist[30005];
queue <int> q;
int main()
{
int i, x, y, d;
fin >> n >> m >> s >> f;
for(i = 1; i <= m; i++)
{
fin >> x >> y >> d;
g[x].push_back({y, d});
g[y].push_back({x, -d});
}
q.push(s);
viz[s] = 1; dist[s] = 0;
while(!q.empty())
{
x = q.front();
q.pop();
for(auto e : g[x])
{
y = e.first;
d = e.second;
if(viz[y] == 0)
{
dist[y] = dist[x] + d;
viz[y] = 1;
q.push(y);
}
}
}
fout << dist[f] << '\n';
return 0;
}