Cod sursa(job #2497010)

Utilizator GhSamuelGherasim Teodor-Samuel GhSamuel Data 21 noiembrie 2019 22:39:43
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 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, pos, 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();

        int length = G[node].size();
        for (int pos = 0; pos < length; ++pos) {
            int newNode = G[node][pos].first;
            int newCost = G[node][pos].second;
            if (!viz[newNode]) {
                viz[newNode] = 1;
                dist[newNode] = dist[node] + newCost;

                if (newNode == fin)
                    break;

                Coada.push(newNode);
            }
        }
    }
    g << dist[fin];
}

int main()
{
    ios_base::sync_with_stdio(false);
    read();
    bfs(start);
    return 0;
}