Pagini recente » Cod sursa (job #1960038) | Cod sursa (job #192803) | Cod sursa (job #939624) | Cod sursa (job #2573035) | Cod sursa (job #2276037)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n,m,X,Y,d[30001];
vector< pair<int,int> >h[30001];
queue<int>q;
int main()
{
fin>>n>>m>>X>>Y;
for(int i=1;i<=m;i++)
{
int x,y,dist;
fin>>x>>y>>dist;
h[x].push_back({y,dist});
h[y].push_back({x,-dist});
}
for(int i=1;i<=n;i++)
d[i]=-1;
d[X]=0;
q.push(X);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0;i<h[x].size();i++)
{
int j=h[x][i].first;
int dist=h[x][i].second;
if(d[j]==-1)
{
d[j]=d[x]+dist;
q.push(j);
if(d[Y]>=0)
{
fout<<d[Y];
return 0;
}
}
}
}
return 0;
}