Cod sursa(job #3181319)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 6 decembrie 2023 20:43:33
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.92 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <deque>
#include <queue>
using namespace std;

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

const int MAX_NODES = 50000;

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

priority_queue<int> currentNodes;

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;
        graph[startNode].push_back(arch);
    }
    minDistance[1] = 0;
    currentNodes.push(1);
    while (currentNodes.empty() == 0) {
        int startNode = currentNodes.top();
        currentNodes.pop();
        for (vector<pair<int, int>>::iterator it = graph[startNode].begin(); it < graph[startNode].end(); ++it) {
            if (minDistance[startNode] + it->second < minDistance[it->first] || minDistance[it->first] == 0) {
                minDistance[it->first] = minDistance[startNode] + it->second;
                currentNodes.push(it->first);
            }
        }
    }
   // 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
 
 8 8
 1 2 1
 1 3 1
 3 4 2
 2 5 2
 4 6 3
 5 7 3
 6 8 4
 7 8 5
 =>
 1 1 3 3 6 6 10
 
 50000 1
 100 200 3
 =>
 0 0 0 0 0 0 0 0 0 ... (de 49.999 ori valoarea 0)
 
 100 10
 2 1 2
 3 1 2
 4 1 2
 5 1 2
 6 1 2
 7 1 2
 8 1 2
 9 1 2
 10 1 2
 11 1 2
 =>
 0 0 0 0 0 0 0 0 0 0 ... (de 99 de ori valoarea 0)
 
 50000 2
 49999 50000 20000
 50000 49999 0
 =>
 0 0 0 0 0 0 0 0 0 0 ... (de 49.999 ori valoarea 0)
 
 8 2
 1 8 2
 6 8 3
 =>
 0 0 0 0 0 0 2
 */