Cod sursa(job #2202641)

Utilizator DordeDorde Matei Dorde Data 9 mai 2018 16:19:46
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 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 = 3e3 + 7;
struct code
{
    int cost , node;
};
vector <code> v [NM2];
queue <int> q;
int n , m , x , y , dp [NM2];
bool mark [NM2];
i64 best ;
int module(int number)
{
    if(number < 0)
        return 0 - number;
    return number;
}
/* int bigger(int value)
{
    int val = best + value;
    int val2 = best - value;
    if(module (val) < module(val2))
        return val;
    return val2;
} */
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;
        for(i = 0 ; i < sz ; ++ i)
            if(! mark [v [curent][i] . node])
            {
                q . push (v [curent][i] . node);
                mark [v [curent][i] . node] = 1;
                if(module (best + v [curent][i] . cost) < minval)
                    minval = best + v [curent][i] . cost , minadd = v [curent][i] . cost;
                if(v [curent][i] . node == y)
                    {
                        g << best + v [curent][i] . cost;
                        return ;
                    }
            }
        best += minadd;
        q . pop ();
    }
    g << -1;
}
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;
}