Pagini recente » Cod sursa (job #945455) | Cod sursa (job #1741995) | Cod sursa (job #2644438) | Cod sursa (job #712674) | Cod sursa (job #2202661)
#include <fstream>
#include <queue>
#include <vector>
#define i64 long long
using namespace std;
char const in [] = "sate.in";
char const out [] = "sate.out";
ifstream f (in);
ofstream g (out);
int const NM = 1e5 + 7 , NM2 = 3e4 + 7;
struct code
{
int cost , node;
};
vector <code> v [NM2];
queue <int> q;
i64 n , m , x , y , dp [NM2];
bool mark [NM2];
i64 best ;
int module(int number)
{
if(number < 0)
return 0 - number;
return number;
}
void bfs ()
{
q . push (x);
mark [x] = 1;
while(q . size ())
{
int curent = q . front () , i , sz = v [curent] . size ();
int minval = 1e9 + 1 , minadd;
bool r = 0;
int val = dp [curent];
for(i = 0 ; i < sz ; ++ i)
if(! mark [v [curent][i] . node])
{
q . push (v [curent][i] . node);
mark [v [curent][i] . node] = 1;
dp [v [curent][i] . node] += (v [curent][i] . cost + val);
}
q . pop ();
}
if(! mark [y])
g << -1 ;
else
g << dp [y];
}
int main()
{
f >> n >> m >> x >> y;
int i;
for(i = 1 ; i <= m ; ++ i)
{
int a , b , c;
code curent;
f >> a >> b >> c;
curent . cost = c;
curent . node = b;
v [a] . push_back (curent);
curent . cost = - c;
curent . node = a;
v [b] . push_back (curent);
}
bfs ();
return 0;
}