Cod sursa(job #2611571)

Utilizator _mirubMiruna-Elena Banu _mirub Data 7 mai 2020 03:56:30
Problema Algoritmul Bellman-Ford Scor 100
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 3.03 kb
#include <bits/stdc++.h>

#define NMAX 50010   
#define oo (1 << 30)
#define NO_PARENT (-1)

using namespace std;

class Task {
public:
  void solve() {
    read_input();
    get_result();
  }

private:
  // n = vertices, m = edges
  int n, m;
  int source;

  // adj[i] contains pairs of neighbouring_node , weight of the edge
  // node -> neighbouring_node
  vector<pair<int, int>> adj[NMAX];

  queue<int> q;

  // d[i] = minimum distance from source to node i
  vector<int> d;

  // p[i] = parent of i on the minimim road from source to i
  vector<int> p;

  void read_input() {
    ifstream fin ("bellmanford.in");
    fin >> n >> m;
    d.resize(n + 1);
    p.resize(n + 1);

    source = 1;

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

    fin.close();
  }

  void get_result() {
    if (BellmanFord(source, d, p)) {
      ofstream fout("bellmanford.out");  
      fout << "Ciclu negativ!";
      fout.close();
    } else {
      print_output();
    }
  }

  bool BellmanFord(int source, vector<int> &d, vector<int> &p) {
    // used[i] = the amount of times the node i has been used
    vector<int> used(n + 1, 0);

    for (int i = 1; i <= n; ++i) {
      // Suppose there is no way to reach i from source
      d[i] = oo;

      // i has no parent
      p[i] = NO_PARENT;
    }

    // Parent of source is 0
    p[source] = 0;

    // Distance from source to source is 0
    d[source] = 0;

    q.push(source);

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

      // Increase the number of uses for node
      ++used[node];
      if (used[node] == n) {
        return true; // Negative cost cycle
      }

      // For every neighbour, the cost from source is relaxed
      for (auto &edge : adj[node]) {
        int neighbour = edge.first;
        int cost_edge = edge.second;

        // If a lower cost is obtained by passing through node
        if (d[node] + cost_edge < d[neighbour]) {

          // Save the new cost
          d[neighbour] = d[node] + cost_edge;

          // The new parent for the neighbour is node
          p[neighbour] = node;

          // Update the cost of the node -> neighbour road
          q.push(neighbour);
        }
      }
    }

    return false;
  }

  // rebuild source -> ... -> node (if exists)
  vector<int> rebuild_path(int node, vector<int> &p) {
    // Cannot reach node from source
    if (p[node] == NO_PARENT) {
      return {};
    }

    // path = {source, ..., node}
    vector<int> path;

    // Rebuild node -> ... -> source (if exists)
    for (; node != 0; node = p[node]) {
      path.push_back(node);
    }

    // Resulted path node -> ... -> source
    // Revert the path
    reverse(path.begin(), path.end());

    return path;
  }

  void print_output() {
    ofstream fout("bellmanford.out");
    for (int i = 1; i <= n; ++i) {
      if (i == source) {
        continue;
      }
      fout << d[i] << " ";
    }
    fout.close();
   }
};

int main() {
  Task *task = new Task();
  task->solve();
  delete task;
  return 0;
}