Pagini recente » Cod sursa (job #1768178) | Cod sursa (job #2673302) | Cod sursa (job #2261056) | Cod sursa (job #1085240) | Cod sursa (job #2275419)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("teatru.in");
ofstream fout ("teatru.out");
vector <pair <int,int> >h[30001];
queue <int> q;
int d[30001], X, Y, n;
///d[i] - distanta de la x la i
int main()
{
int i, m, x, y, dist, j;
cin >> n >> m >> X >> y;
for(i = 1; i <= m; i++)
{
cin >> x >> y >> dist;
h[x].push_back({y, dist});
h[y].push_back({x, -dist});
}
for(i = 1; i <= n; i++)
d[i] = -1;
d[x] = 0;
q.push(x);
while(!q.empty())
{
x = q.front();
q.pop();
for(auto w : h[x])
{
j = w.first;
dist = w.second;
if(d[j] == -1)
{
d[j] = d[x] + dist;
q.push(j);
if(d[Y] >= 0) {cout << d[Y]; return 0;}
}
}
}
return 0;
}