Cod sursa(job #3189331)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 4 ianuarie 2024 20:57:23
Problema Algoritmul lui Dijkstra Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.44 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<pair<int, 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({0, 1});
    while (currentNodes.empty() == 0) {
        int startNode = currentNodes.top().second;
        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({minDistance[it->first], it->first});
            }
        }
    }
    for (int i = 2; i <= noNodes; ++i) {
        fout << minDistance[i] << ' ';
    }
   /* priority_queue<pair<int, int>> s;
    s.push({5, 4});
    s.push({6, 2});
    s.push({1, 3});
    cout << s.top().first << ' ' << s.top().second; */
    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
 */