Cod sursa(job #3171416)

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

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

const int MAX_NODES = 50000;

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

int minDistance[MAX_NODES + 1];

void gen(int noNodes, int startNode) {
    for (vector<pair<int, int>>::iterator it = graph[startNode].begin(); it < graph[startNode].end(); ++it) {
        if (minDistance[it->first] == 0) {
            minDistance[it->first] = minDistance[startNode] + it->second;
        } else {
            minDistance[it->first] = min(minDistance[startNode] + it->second, minDistance[it->first]);
        }
        gen(noNodes, it->first);
    }
}

int main() {
    int noNodes, noArches;
    fin >> noNodes >> noArches;
    for (int i = 1; i <= noArches; ++i) {
        int start, end, distance;
        fin >> start >> end >> distance;
        pair<int, int> info;
        info.first = end;
        info.second = distance;
        graph[start].push_back(info);
    }
    gen(noNodes, 1);
    for (int i = 2; i <= noNodes; ++i) {
        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
 */