Cod sursa(job #2029570)

Utilizator razvan99hHorhat Razvan razvan99h Data 30 septembrie 2017 11:40:06
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 30005
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y, a, b, d, dist[NMAX], viz[NMAX], inq[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();
        inq[nod] = 0;
        for(auto it : g[nod]){
            if(!viz[it.first] && !inq[it.first]){
                viz[it.first] = 1;
                dist[it.first] = dist[nod] + it.second;
                q.push(it.first);
                inq[it.first] = 1;
            }
        }
    }
}
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;
}