Cod sursa(job #2198487)

Utilizator marian98Horodnic Gheorghe Marian marian98 Data 24 aprilie 2018 15:47:13
Problema Algoritmul Bellman-Ford Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 2.97 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

#define MAXN 50001

class EdgeTo {
public:
    int node;
    int cost;

    EdgeTo(int node, int cost) {
        this->node = node;
        this->cost = cost;
    }
};

class Edge {
public:
    int u;
    int v;
    int cost;

    Edge(int u, int v, int c) {
        this->u = u;
        this->v = v;
        this->cost = c;
    }
};

class Compare {
public:
    bool operator() (EdgeTo a, EdgeTo b) {
        return a.cost > b.cost;
    }
};

int n, m;
vector<vector<EdgeTo>> adj(MAXN); 
vector<Edge> all_edges;
vector<long> distances;
bool found_cycles = false;

void read() {
    fstream fin("bellmanford.in", ios::in);
    fin >> n >> m;

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

    fin.close();
}

void bellman_ford(int source) {
    /*for (int i = 0; i <= n; i++) {
        distances.push_back(1 << 29);
    }
    distances[source] = 0;

    for (int i = 1; i <= n; i++) {
        for (Edge edge : all_edges) {
            if (distances[edge.v] > distances[edge.u] + edge.cost) {
                distances[edge.v] = distances[edge.u] + edge.cost;
            }
        }
    }

    for (Edge edge : all_edges) {
        if (distances[edge.v] > distances[edge.u] + edge.cost) {
            found_cycles = true;
            break;
        }
    }*/

    priority_queue<EdgeTo, vector<EdgeTo>, Compare> heap;
    vector<bool> visited;

    for (int i = 0; i <= n; i++) {
        distances.push_back(1 << 30);
        visited.push_back(false);
    }
    distances[source] = 0;
    visited[source] = true;

    heap.push(EdgeTo(source, 0));

    while (!heap.empty()) {
        int node = heap.top().node;
        int d = heap.top().cost;
        heap.pop();

        visited[node] = true;

        if (d > distances[node]) {
            continue;
        }

        for (EdgeTo neighbour : adj[node]) {
            if (distances[neighbour.node] > distances[node] + neighbour.cost) {
                distances[neighbour.node] = distances[node] + neighbour.cost;
                heap.push(EdgeTo(neighbour.node, distances[neighbour.node]));

                if (visited[neighbour.node] == true) {
                    found_cycles = true;
                    return;
                }
            }
        }
    }
}

void write() {
    fstream fout("bellmanford.out", ios::out);
    if (found_cycles == false) {
        for (int i = 2; i <= n; i++) {
            if (distances[i] == 1 << 29) {
                fout << 0 << " ";   
            } else {
                fout << distances[i] << " ";
            }
        }
    } else {
        fout << "Ciclu negativ!";
    }

    fout.close();
}

int main() {
    read();
    bellman_ford(1);
    write();
    return 0;
}