Cod sursa(job #2171373)

Utilizator liviu2000Dragomirescu Liviu liviu2000 Data 15 martie 2018 12:07:23
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

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

int n , m , dist[50001] ;
vector<pair<int,int> > graf[50001] ;

class compar
{
public:
    bool operator () ( const int &a , const int &b )
    {
        return dist[a] > dist[b] ;
    }
};
void citire()
{
    int i , x , y , c ;
    fin >> n >> m ;
    for ( i = 1 ; i <= m ; i++ )
    {
        fin >> x >> y >> c ;
        graf[x].push_back(make_pair(y,c)) ;
        graf[y].push_back(make_pair(x,c)) ;
    }
}

void dijkstra()
{
    int i , nod ;
    priority_queue<int,vector<int> , compar > pq ;
    pq.push(1) ;
    memset(dist,0x3f3f3f3f,4*n+4) ;
    dist[1] = 0 ;
    while(!pq.empty())
    {
        nod = pq.top() ;
        pq.pop() ;
        for ( i = 0 ; i < graf[nod].size() ; i++ )
        {
            if ( dist[graf[nod][i].first] > dist[nod] + graf[nod][i].second )
            {
                dist[graf[nod][i].first] = dist[nod] + graf[nod][i].second ;
                pq.push(graf[nod][i].first) ;
            }
        }
    }
    for ( i = 2 ; i <= n ; i++ )
        fout << dist[i] << " " ;
}

int main()
{
    citire() ;
    dijkstra() ;
}