Cod sursa(job #2202661)

Utilizator DordeDorde Matei Dorde Data 9 mai 2018 16:48:04
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#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;
}