Cod sursa(job #1726698)

Utilizator cristina_borzaCristina Borza cristina_borza Data 8 iulie 2016 18:57:31
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 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");

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

bool c[NMAX];

vector <vector <pair <int , int> > > G;
queue <int> coada;

void bfs(int node);

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

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

        G[x].push_back(make_pair(y , cst));
        G[y].push_back(make_pair(x , cst));
    }

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

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

        c[node] = 1;
        int nr = G[node].size();

        for (int i = 0; i < nr; ++i) {
            int it = G[node][i].first;
            int cost = G[node][i].second;

            if (c[it])
                continue;

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

            else {
                dp[it] = dp[node] - cost;
            }

            coada.push(it);
        }
    }
}