Cod sursa(job #2029533)

Utilizator razvan99hHorhat Razvan razvan99h Data 30 septembrie 2017 11:26:45
Problema Sate Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 30000
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y, a, b, d, dist[NMAX], viz[NMAX];
vector < pair<int, int> > g[NMAX];
queue <int> q;

void bfs(int st){
    q.push(st);
    dist[st] = 0;
    viz[st] = 1;
    while(!q.empty()){
        int nod = q.front();
        q.pop();
        for(auto it : g[nod]){
            if(!viz[it.first]){
                viz[it.first] = 1;
                dist[it.first] = dist[nod] + it.second;
                q.push(it.first);
            }
        }
    }
}
int main()
{
    fin >> n >> m >> x >> y;
    for(int i = 1; i <= m; i++){
        fin >> a >> b >> d;
        g[a].push_back({b, d});
        g[b].push_back({a, -d});
    }
    bfs(x);
    fout << dist[y];
    return 0;
}