Cod sursa(job #1721272)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 25 iunie 2016 00:58:04
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 30005;

class hpstream {
private:
    static const int BMAX = 50005;

    FILE *file;
    char  data[BMAX];
    int   buff;

    inline char nextch(void) {
        if(buff==BMAX) {
            buff = 0;
            fread(data, 1, BMAX, file);
        }
        return data[buff++];
    }

public:
    hpstream(const char *str) {
        file = fopen(str, "r");
        buff = BMAX;
    }

    hpstream &operator >>(int &arg) {
        char ch;

        while(!isdigit(ch = nextch()));
        arg = ch - '0';

        while( isdigit(ch = nextch()))
            arg = arg*10+ch-'0';

        return *this;
    }

    void close(void) {
        fclose(file);
    }
};

int  y;
int  d[NMAX];
bool f[NMAX];

vector<pair<int, int> > g[NMAX];

void dfs(int u) {
    f[u] = 1;
    for(auto i:g[u])
    if(!f[i.first]) {
        if(i.first > u)
            d[i.first] = d[u] + i.second;
        else
            d[i.first] = d[u] - i.second;
        dfs(i.first);
    }
}

int main(void) {
    hpstream fi("sate.in");
    ofstream fo("sate.out");
    int n, m, a, b, c, x;

    fi>>n>>m>>x>>y;

    if(n==13) cout<<"Aoleu!\n";
    if(n==7)  cout<<"Norocu' nostru!\n";

    while(m--) {
        fi>>a>>b>>c;
        g[a].push_back(make_pair(b, c));
        g[b].push_back(make_pair(a, c));
    }

    dfs(x);

    fo<<d[y]<<'\n';

    fi.close();
    fo.close();
    return 0;
}