Pagini recente » Clasament simulareoni2007 | Cod sursa (job #117360) | Cod sursa (job #1286376) | Cod sursa (job #927066) | Cod sursa (job #1120191)
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
queue < int > q;
vector < pair < int, int > > L[30010];
int cost[30010],x,y,c,n,m,ic,sf,i;
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&ic,&sf);
while(m--)
{
scanf("%d%d%d",&x,&y,&c);
L[x].push_back(make_pair(y,c));
L[y].push_back(make_pair(x,-c));
}
q.push(ic); cost[ic]=1;
while(!q.empty())
{
x=q.front();
for(i=0; i<L[x].size(); ++i)
if(cost[L[x][i].first]==0)
{
cost[L[x][i].first]=cost[x]+L[x][i].second;
if(L[x][i].first==sf)
{
printf("%d\n",cost[sf]-1);
return 0;
}
q.push(L[x][i].first);
}
q.pop();
}
return 0;
}