Cod sursa(job #863294)

Utilizator cernat.catallinFMI Cernat Catalin Stefan cernat.catallin Data 23 ianuarie 2013 18:17:21
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <queue>
using namespace std;

#define inf 21000000

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

struct nod_g{
    int nod, cost;
};
vector < list<nod_g> > graf;
vector <unsigned int> cost;
list <nod_g>::iterator it;

int n, m, x, y;
nod_g temp;

void citire(){
    int x1, y1, c;
    f >> n >> m >> x >> y;
    graf.resize(n+1);
    cost.resize(n+1);

    for(int i = 1; i <= m; ++i){
        f >> x1 >> y1 >> c;
        temp.nod = y1; temp.cost = c;
        graf[x1].push_back(temp);
        temp.nod = x1; temp.cost = -c;
        graf[y1].push_back(temp);
    }
    f.close();
}

void bfs(){
    int first = x;
    queue <unsigned int> coada;
    coada.push(x);

    while(!coada.empty()){
        for(it = graf[first].begin(); it != graf[first].end(); ++it)
            if(!cost[it->nod]){
                coada.push(it->nod);
                cost[it->nod] = cost[first] + it->cost;
                if(it->nod == y){
                    g << cost[y];
                    return;
                }
            }
        coada.pop();
        first = coada.front();
    }
}

int main(){
    citire();
    bfs();
    g.close();

    return 0;
}