Cod sursa(job #1911671)

Utilizator MailatMailat Radu Mailat Data 7 martie 2017 21:22:01
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include <bits/stdtr1c++.h>
#define maxn 30010
using namespace std;

ofstream fout("sate.out");

int Cost[maxn], V[maxn], S[maxn];
vector <int> A[maxn];
vector <int> D[maxn];
int N, M, X, Y;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

void init() {
    InParser fin("sate.in");
    fin >> N >> M >> X >> Y;
    int x, y ,c;
    for(int i=0; i < M; i++) {
        fin >> x >> y >> c;
        A[x].push_back(y);
        D[x].push_back(c);
        A[y].push_back(x);
        D[y].push_back(-c);
    }
    Cost[X] = 0;
}

void BFS(int nod) {
    memset(V, -1, sizeof(V));
    int L=1;
    S[L] = nod;
    V[nod] = 1;
    for(int i=1; i<=L;i++) {
        for(int j=0; j < A[S[i]].size(); j++) {
            if(V[A[S[i]][j]] == -1) {
                S[++L] = A[S[i]][j];
                V[A[S[i]][j]] = 1;
                Cost[A[S[i]][j]] = Cost[S[i]] + D[S[i]][j];
                if(A[S[i]][j] == Y) {
                    i = L;
                    j = A[S[i]].size();
                    break;
                }
            }
        }
    }
}

int main()
{
    init();
    BFS(X);
    fout << Cost[Y];
    return 0;
}