Cod sursa(job #2323439)

Utilizator CryshanaGanea Carina Cryshana Data 19 ianuarie 2019 10:54:42
Problema Sate Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream in  ("sate.in" );
ofstream out ("sate.out");

const int N = 30001;
vector < pair <int, int> > a[N];
int q[N];
int scor[N];
bool viz[N];

int main()
{
    int n, m, x, y, cap = 0 , coada = -1;
    in >> n >> m >> x >> y;
    for ( int i = 0; i < m; i++ )
    {
        pair < int, int > x1, x2;
        in >> x1.first >> x2.first >> x2.second;
        x1.second = x2.second;
        a[x1.first].push_back(x2);
        a[x2.first].push_back(x1);
    }
    q[++coada] = x;
    while ( cap <= coada )
    {
        int  x = q[cap++];
        viz[x] = true;
        for ( auto p : a[x])
        {
            if ( !viz[p.first] )
            {
                q[++coada] = p.first;
                if ( x < p.first )
                {
                    scor[p.first] = scor[x] + p.second;
                }
                else
                {
                    scor[p.first] = scor[x] - p.second;
                }
            }
        }
    }
    out << scor[y];
    return 0;
}