Cod sursa(job #2755577)

Utilizator Casian_doispeChiriac Casian Casian_doispe Data 27 mai 2021 18:27:01
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.96 kb
#include <fstream>
#include <vector>
#include <deque>
#include <cstring>
#include <queue>
#include <limits.h>
#include <unordered_set>

using namespace std;
ifstream cin("dijkstra.in") ;
ofstream cout("dijkstra.out") ;

int n ;

vector<pair<int, int> > v[50009] ;

int cost[50009], tatal[50009], verif[50009] ;

int main()
{

    int q ;

    cin >> n >> q ;

    while(q --)
    {

        int a, b, c ;

        cin >> a >> b >> c ;

        v[a].push_back({b, c}) ;

    }

    for(int f = 1 ; f <= n ; f ++)
        cost[f] = INT_MIN ;
/*
    int vf = 1 ;

    cost[1] = 0 ;

    viz[1] = 1 ;

    while(vf)
    {

        int mnf = 0 ;

        for(int f = 1 ; f < v[vf].size() ; f ++)
            if(v[vf][f].second < v[vf][mnf].second)mnf = f ;

        mnf = v[vf][mnf].first ;

        /// mnf este nodul in care mergem de aici



        cost[mnf]

    }
*/
    priority_queue<pair<int, int> > pq ;

    pq.push({0, 1}) ; /// intai cost si dupa nod

    cost[1] = 0 ;

    while(pq.size())
    {

        int nod_val = pq.top().first ;
        int nod_curent = pq.top().second ;

        pq.pop() ;

        if(!verif[nod_curent])
        {

        verif[nod_curent] = 1 ;

        for(int f = 0 ; f < v[nod_curent].size() ; f ++) /// extindem pathul acesta
        {

            int nod_destinatie = v[nod_curent][f].first ;
            int cost_local = v[nod_curent][f].second ;

            /// trebuie vazut daca nod_val + cost_local este mai mic ca cost

            if(nod_val - cost_local > cost[nod_destinatie])
            {

                cost[nod_destinatie] = nod_val - cost_local ;

                pq.push({cost[nod_destinatie], nod_destinatie}) ;

            }

        }

        }

    }

    for(int f = 1 ; f <= n ; f ++)
        if(cost[f] == INT_MIN)cost[f] = 0 ;

    for(int f = 2 ; f <= n ; f ++)
        cout << cost[f] * -1 << " " ;

    return 0;
}