Cod sursa(job #1876659)

Utilizator dsergiu05Sergiu Druga dsergiu05 Data 12 februarie 2017 15:25:47
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <vector>

using namespace std;

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

struct str {
    int x, y;
};

const int nmax=30000, inf=1e9;
vector <str> g[nmax+1];
int sol[nmax+1];

void dfs (int x) {
    for (int i=0; i<int(g[x].size()); i++) {
        int xn=g[x][i].x;
        if (sol[xn]==inf) {
            sol[xn]=sol[x]+g[x][i].y;
            dfs(xn);
        }
    }
}

int main () {
    int n, m, a, b;
    fin>>n>>m>>a>>b;

    for (int i=1; i<=m; i++) {
        int x, y, c;
        fin>>x>>y>>c;
        str aux;
        aux.x=y;
        aux.y=c;
        g[x].push_back(aux);
        aux.x=x;
        aux.y=-c;
        g[y].push_back(aux);
    }

    for (int i=1; i<=nmax; i++) {
        sol[i]=inf;
    }
    sol[a]=0;
    dfs(a);

    fout<<sol[b]<<"\n";

    return 0;
}