Cod sursa(job #1726696)

Utilizator cristina_borzaCristina Borza cristina_borza Data 8 iulie 2016 18:49:50
Problema Sate Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <vector>
#include <queue>
#include <map>
#include <set>

#define NMAX 300005

using namespace std;

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

long long n , m , st , en , x , y , cst;
long long dp[NMAX];

bool c[NMAX];

map <pair <long long , int> , int> cost;
vector <vector <int> > G;
queue <int> coada;

void bfs(long long node);

int main() {
    f >> n >> m >> st >> en;
    G.resize(n + 5);

    for (long long i = 1; i <= m; ++i) {
        f >> x >> y >> cst;

        cost[make_pair(x , y)] = cst;
        cost[make_pair(y , x)] = cst;

        G[x].push_back(y);
        G[y].push_back(x);
    }

    bfs(st);
    g << dp[en];
    return 0;
}

void bfs(long long node) {
    coada.push(node);
    while (!coada.empty()) {
        long long node = coada.front();
        coada.pop();

        c[node] = 1;
        for (vector <int> :: iterator it = G[node].begin(); it != G[node].end(); ++it) {
            if (c[*it])
                continue;

            if (*it > node) {
                dp[*it] = dp[node] + cost[make_pair(node , *it)];
            }

            else {
                dp[*it] = dp[node] - cost[make_pair(node , *it)];
            }

            if (dp[*it] < 0) {
                dp[*it] = -dp[*it];
            }

            coada.push(*it);
        }
    }
}