Pagini recente » Cod sursa (job #801321) | Cod sursa (job #417754) | Cod sursa (job #2913204) | Cod sursa (job #910268) | Cod sursa (job #826210)
Cod sursa(job #826210)
#include <fstream>
#include <vector>
#include <queue>
#define INF 24000000
using namespace std;
const char iname[] = "sate.in";
const char oname[] = "sate.out";
ifstream fin(iname);
ofstream fout(oname);
int i , j , x , y , D;
int N , M , X , Y;
int cost[ 30008 ];
struct sat
{
int val;
int c;
};
vector <sat> v[ 30008 ];
queue <int> Q;
sat nod;
int main()
{
fin >> N >> M >> X >> Y;
for ( i = 1; i <= N; ++i ) cost[ i ] = INF;
for ( i = 1; i <= M; ++i )
{
fin >> x >> y >> nod.c;
nod.val = y; v[ x ].push_back( nod );
nod.c *= -1;
nod.val = x; v[ y ].push_back( nod );
}
Q.push( X );
cost[ X ] = 0;
while ( !Q.empty() )
{
x = Q.front();
Q.pop();
for ( i = 0; i < v[ x ].size(); ++i )
{
if ( cost[ x ] + v[ x ][ i ].c < cost[ v[ x ][ i ].val ] )
{
cost[ v[ x ][ i ].val ] = cost[ x ] + v[ x ][ i ].c;
Q.push( v[ x ][ i ].val );
}
}
if ( cost[ Y ] > 0 && cost[ Y ] != INF )
{
fout << cost[ Y ] << '\n';
return 0;
}
}
return 0;
}