Cod sursa(job #3300418)

Utilizator AndreiLeusteanLeustean Andrei AndreiLeustean Data 15 iunie 2025 16:05:15
Problema Algoritmul Bellman-Ford Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.85 kb
#include <iostream> // Include iostream for input/output operations
#include <vector> // Include vector for dynamic arrays
#include <limits> // Include limits for numeric limits (e.g., infinity)

using namespace std;

// Define a struct to represent an edge in the graph
// T is a template parameter allowing different weight types (e.g., int, long long)
template <typename T>
struct Edge {
    int u; // Source vertex index
    int v; // Destination vertex index
    T weight; // Weight of the edge
};

// Bellman-Ford algorithm implementation
template <typename T>
bool bellmanFord(vector<Edge<T>> &edges, int V, int src, vector<T> &dist) {
    // V: Number of vertices in the graph
    // src: Index of the source vertex
    // dist: Vector to store shortest distances from the source vertex

    // Initialize infinity value for the weight type T
    T INF = numeric_limits<T>::max(); 

    // Initialize distances to infinity, except for the source vertex
    dist.assign(V, INF); 
    dist[src] = 0;

    // Iterate V-1 times to relax all edges
    for (int i = 1; i < V; i++) { 
        for (auto &e : edges) { // Iterate through all edges
            // If a shorter path is found, update the distance
            if (dist[e.u] != INF && dist[e.u] + e.weight < dist[e.v]) {
                dist[e.v] = dist[e.u] + e.weight;
            }
        }
    }

    // Check for negative-weight cycles after V-1 iterations
    for (auto &e : edges) {
        // If a shorter path is still found after V-1 iterations, a negative cycle exists
        if (dist[e.u] != INF && dist[e.u] + e.weight < dist[e.v]) {
            return false; // Negative cycle detected
        }
    }
    return true; // No negative cycles found
}

int main() {
    int n, m; // n: Number of vertices, m: Number of edges

    // Read the number of vertices and edges from input
    cin >> n >> m;

    // Create a vector of edges
    vector<Edge<long long>> edges(m); 
    for (int i = 0; i < m; ++i) {
        // Read edge information (u, v, weight) from input
        cin >> edges[i].u >> edges[i].v >> edges[i].weight;
        // Adjust vertex indices to be 0-based
        edges[i].u--; 
        edges[i].v--; 
    }

    vector<long long> dist; // Vector to store shortest distances

    // Run Bellman-Ford algorithm
    if (bellmanFord(edges, n, 0, dist)) {
        // If no negative cycle is found, print the shortest distances
        for (int i = 1; i < n; ++i) {
            if (dist[i] == numeric_limits<long long>::max()) {
                cout << "INF "; // Print INF if distance is infinity
            } else {
                cout << dist[i] << " "; // Print the distance
            }
        }
        cout << endl;
    } else {
        cout << "Ciclu negativ!" << endl; // Print message if negative cycle is detected
    }

    return 0;
}