Cod sursa(job #1324651)

Utilizator StexanIarca Stefan Stexan Data 22 ianuarie 2015 17:12:50
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

#define NMAX 100005
#define TYPE pair<int,int>

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

int N,M,Source,Destination;
int Value[NMAX];
vector<TYPE> G[NMAX];
queue<TYPE> Q;

void Init(){
    for (int i = 0; i <= NMAX; i++) {
        Value[i] = -1;
    }
}

void Read(){
    f>>N>>M>>Source>>Destination;
    for (int i = 0; i < M; i++) {
        int x,y,s;
        f>>x>>y>>s;
        G[x].push_back(make_pair(y, s));
        G[y].push_back(make_pair(x, -s));
    }
}

void BFS(){
    Q.push(make_pair(Source, 0)); Value[Source] = 0;
    
    while (!Q.empty()) {
        int Element = Q.front().first; Q.pop();
        for (vector<TYPE>::iterator it = G[Element].begin(); it != G[Element].end(); it++) {
            pair<int, int> Sel = *it;
            if (Value[Sel.first] == -1) {
                Value[Sel.first] = Value[Element] + Sel.second;
                if (Sel.first == Destination) {
                    return;
                }
                Q.push(*it);
            }
        }
    }
    
}

void Solve(){
    BFS();
}

void Write(){
    g<<Value[Destination];
}

int main() {
    
    Init();
    Read();
    Solve();
    Write();
    
    return 0;
}