Cod sursa(job #1470398)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 10 august 2015 23:37:35
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>

using namespace std;

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

const int Nmax = 30005;
const int INF = 1e9;

struct conexion{
    int go, len;
};

int g[Nmax];

deque < int > stak;
vector < conexion > G[Nmax];

void bfs(){
    int node;
    while(!stak.empty()){
        node = stak.front();
        stak.pop_front();
        for(int i = 0; i < G[node].size(); i++){
            if(G[node][i].len + g[node] < g[G[node][i].go]){
                g[G[node][i].go] = G[node][i].len + g[node];
                stak.push_back(G[node][i].go);
            }
        }
    }
}

int main(){
    int n, m, x, y, a, b, d;
    conexion A;
    fin >> n >> m >> x >> y;
    for(int i = 1; i <= m; i++){
        fin >> a >> b >> d;
        A.go = b; A.len = d;
        G[a].push_back(A);
        A.go = a; A.len = -d;
        G[b].push_back(A);
    }
    for(int i = 1; i <= n; i++){
        g[i] = INF;
    }
    g[x] = 0;
    stak.push_back(x);
    bfs();
    fout << g[y];
    return 0;
}