Pagini recente » Cod sursa (job #1820109) | Cod sursa (job #1120351) | Cod sursa (job #593204) | Cod sursa (job #2436129) | Cod sursa (job #2275428)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector <pair<int, int> > h[30001];
queue<int> Q;
int d[30001],X,Y; /// d[i] = distanta de la x la i
int main()
{
int i,j,dist,m,n,x,y;
fin>>n>>m>>X>>Y;
for(i=1; i<=m; i++)
{
fin>>x>>y>>dist;
/// pun in h[x] perechea 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)
{
fout<<d[Y];
return 0;
}
}
return 0;
}