Cod sursa(job #3322749)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 15 noiembrie 2025 15:02:32
Problema Algoritmul Bellman-Ford Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <fstream>
#include <bitset>
#include <queue>
#include <map>
using namespace std;

ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");

vector<pair<int, int>> graph[100001];

long long minDist[100001];

void bfs(int startNode) {
    queue<int> q;
    q.push(startNode);
    minDist[startNode] = 0;
    while (!q.empty()) {
        int currentNode = q.front();
        q.pop();
        for (vector<pair<int, int>>::iterator it = graph[currentNode].begin(); it != graph[currentNode].end(); ++it) {
            if (minDist[currentNode] + it->second < minDist[it->first]) {
                minDist[it->first] = minDist[currentNode] + it->second;
                q.push(it->first);
            }
        }
    }
}

int main() {
    int n, m;
    fin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int x, y, c;
        fin >> x >> y >> c;
        graph[x].push_back({y, c});
    }
    for (int i = 1; i <= n; ++i) {
        minDist[i] = 1e18;
    }
    bfs(1);
    for (int i = 2; i <= n; ++i) {
        fout << minDist[i] << ' ';
    }
    return 0;
}
/*
 
 0 0 0 5 6
 7 7 1 1 1
 1 1 1 3 1
 1 1 2 2 1
 0 0 9 0 0
 0 0 0 0 9
 
 
 */