Pagini recente » Cod sursa (job #2458293) | Cod sursa (job #3220123) | Cod sursa (job #1626943) | Cod sursa (job #2759617) | Cod sursa (job #1597613)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define Nmax 30010
vector <pair<int,int> >ad[Nmax] ;
queue <int> q ;
bool b[Nmax] ;
int T[Nmax] , n , m , x , y , d , e , f;
void bfs ( int start , int sf )
{
q.push(start) ;
b[start] = true ;
T[start] = 0 ;
while ( !q.empty() )
{
//if ( T[sf] > 0 ) return ;
int nod = q.front();
q.pop();
b[nod] = true ;
for ( int i = 0 ; i < ad[nod].size() ; ++i )
{
if ( !b[ad[nod][i].first] )
{
T[ad[nod][i].first] = T[nod] + ad[nod][i].second ;
q.push(ad[nod][i].first);
b[ad[nod][i].first] = true ;
}
}
}
}
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d %d %d %d",&n,&m,&x,&y);
for ( int i = 1 ; i <= m ; i++ )
{
scanf("%d %d %d",&d,&e,&f);
ad[d].push_back(make_pair(e,f));
ad[e].push_back(make_pair(d,-f));
}
bfs(x,y) ;
printf("%d ",T[y]);
}