Cod sursa(job #3172238)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 20 noiembrie 2023 12:58:38
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.91 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

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

const int MAX_NODES = 50000;
const long long MAX_DISTANCE = 5000000000;

vector<pair<int, int>> graph[MAX_NODES + 1];

bitset<MAX_NODES + 1> visitedNodes;

long long minDistance[MAX_NODES + 1];

int main() {
    int noNodes, noArches;
    fin >> noNodes >> noArches;
    for (int i = 1; i <= noArches; ++i) {
        int startNode;
        pair<int, int> arch;
        fin >> startNode >> arch.first >> arch.second;
        minDistance[startNode] = MAX_DISTANCE + 1;
        minDistance[arch.first] = MAX_DISTANCE + 1;
        graph[startNode].push_back(arch);
    }
    minDistance[1] = 0;
    bool allNodesVerified = 0;
    while (allNodesVerified == 0) {
        allNodesVerified = 1;
        for (int i = 1; i <= noNodes; ++i) {
            if (visitedNodes[i] == 0 && minDistance[i] != MAX_DISTANCE + 1) {
                allNodesVerified = 0;
                for (vector<pair<int, int>>::iterator it = graph[i].begin(); it < graph[i].end(); ++it) {
                    minDistance[it->first] = min(minDistance[i] + it->second, minDistance[it->first]);
                }
                visitedNodes[i] = 1;
            }
        }
    }
    for (int i = 2; i <= noNodes; ++i) {
        if (minDistance[i] == MAX_DISTANCE + 1) {
            minDistance[i] = 0;
        }
        fout << minDistance[i] << ' ';
    }
    return 0;
}
/*
 5 6
 1 2 1
 1 4 2
 4 3 4
 2 3 2
 4 5 3
 3 5 6
 =>
 1 3 2 5
 
 5 6
 1 4 10
 1 3 9
 1 2 8
 4 5 9
 3 5 10
 2 5 11
 =>
 8 9 10 19
 
 7 7
 1 4 10
 1 3 9
 1 2 8
 4 5 9
 3 5 10
 2 5 11
 6 7 100
 =>
 8 9 10 19 0 0
 
 3 1
 2 3 20000
 =>
 0 0
 
 4 2
 1 2 0
 1 3 0
 =>
 0 0 0
 
 4 4
 1 2 0
 1 3 2
 1 4 3
 2 4 0
 =>
 0 2 0
 
 4 4
 1 2 0
 1 3 0
 1 4 0
 2 4 0
 =>
 0 0 0
 
 5 4
 2 3 0
 3 4 0
 2 4 0
 2 5 0
 =>
 0 0 0 0
 
 5 4
 2 3 6
 3 4 5
 2 4 4
 2 5 3
 =>
 0 0 0 0
 */