Cod sursa(job #2497024)

Utilizator GhSamuelGherasim Teodor-Samuel GhSamuel Data 21 noiembrie 2019 22:56:52
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>
#define Nmax 30005
using namespace std;
FILE *f = fopen("sate.in", "r");
FILE *g = fopen("sate.out", "w");

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

void read()
{
    fscanf(f, "%d%d%d%d", &n, &m, &start, &fin);

    int x, y, c;
    for (int i = 1; i <= m; ++i) {
        fscanf(f, "%d%d%d", &x, &y, &c);
        G[x].push_back(make_pair(y, c));
        G[y].push_back(make_pair(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();

        for (auto pos : G[node]) {
            int newNode = pos.first;
            int newCost = pos.second;
            if (!viz[newNode]) {
                viz[newNode] = 1;
                dist[newNode] = dist[node] + newCost;

                if (newNode == fin)
                    break;

                Coada.push(newNode);
            }
        }
    }
    fprintf(g, "%d", dist[fin]);
}

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