Cod sursa(job #819380)

Utilizator alexclpAlexandru Clapa alexclp Data 18 noiembrie 2012 21:23:45
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <fstream>
#include <queue>
#include <vector>

#define N 30005

using namespace std;

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

struct Muchie {
    int varf;
    int cost;
};

Muchie getMuchie (int a, int b)
{
    Muchie muchie;
    muchie.varf = a;
    muchie.cost = b;
    return muchie;
}

int n, m, start, final;
vector <Muchie> a[N];
queue <int> coada;
int cost[N];

void lee()
{
    coada.push(start);
    while(!coada.empty()) {
        int element = coada.front();
        coada.pop();
        for (int i = 0; i < a[element].size(); i++) {
            int muchie = a[element][i].varf;
            int costDrum  = a[element][i].cost;
            int costNou = cost[element];
            if (muchie < element) {
                costNou -= costDrum; // cand se merge la stanga se scade
            } else {
                costNou += costDrum; // cand se merge la dreapta se creste
            }
            if (cost[muchie] < costNou or cost[muchie] == -1) {
                cost[muchie] = costNou;
                coada.push(muchie);
            }
        }
    }
}

void read()
{
    in >> n >> m >> start >> final;
    for (int i = 1; i <= m; i++) {
        int sat1, sat2, costus;
        in >> sat1 >> sat2 >> costus;
        a[sat1].push_back(getMuchie(sat2, costus));
        a[sat2].push_back(getMuchie(sat1, costus));
    }
    for (int i = 1; i <= n; i++) {
        cost[i] = -1;
    }
}

int main()
{
    read();
    lee();
    out << cost[final] + 1 << "\n";
    
    
    return 0;
}