Cod sursa(job #2497002)

Utilizator GhSamuelGherasim Teodor-Samuel GhSamuel Data 21 noiembrie 2019 22:29:51
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
#define Nmax 30005
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");

int n, m, dist[Nmax], start, fin, viz[Nmax];
vector <pair <int, int> > G[Nmax];

void read()
{
    f >> n >> m >> start >> fin;

    int x, y, c;
    for (int i = 1; i <= m; ++i) {
        f >> x >> y >> c;
        G[x].push_back({y, c});
        G[y].push_back({x, -c});
    }
}

void bfs(int start)
{
    queue <int> Coada;
    Coada.push(start);
    dist[start] = 0;
    viz[start] = 1;

    while (!Coada.empty()) {
        int node = Coada.front();
        Coada.pop();

        vector <pair <int, int> >::iterator it;
        for (it = G[node].begin(); it != G[node].end(); ++it) {
            int newNode = (*it).first;
            int newCost = (*it).second;
            if (!viz[newNode]) {
                viz[newNode] = 1;
                dist[newNode] = dist[node] + newCost;
                Coada.push(newNode);
            }
        }
    }
    g << dist[fin];
}

int main()
{
    read();
    bfs(start);
    return 0;
}