Cod sursa(job #3175918)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 26 noiembrie 2023 15:44:25
Problema Algoritmul lui Dijkstra Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.05 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <deque>
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];

deque<int> currentNodes;
deque<int> nextNodes;

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[i] = MAX_DISTANCE + 1;
        graph[startNode].push_back(arch);
    }
    minDistance[1] = 0;
    currentNodes.push_back(1);
    while (currentNodes.size() > 0) {
        for (deque<int>::iterator it = currentNodes.begin(); it < currentNodes.end(); ++it) {
            for (vector<pair<int, int>>::iterator it2 = graph[*it].begin(); it2 < graph[*it].end(); ++it2) {
                if (minDistance[*it] + it2->second < minDistance[it2->first]) {
                    minDistance[it2->first] = minDistance[*it] + it2->second;
                    nextNodes.push_back(it2->first);
                }
            }
        }
        currentNodes = nextNodes;
        nextNodes.clear();
    }
    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
 
 4 4
 1 2 20000
 2 3 20000
 3 2 20000
 2 4 20000
 =>
 20000 40000 40000
 
 5 6
 1 5 20
 1 2 1
 2 3 2
 3 4 3
 4 5 4
 5 1 5
 =>
 1 3 6 10
 */