Cod sursa(job #1706526)

Utilizator catu_bogdan_99Catu Bogdan catu_bogdan_99 Data 22 mai 2016 18:51:36
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <vector>
#include <queue>
#define DIM 50005
#define INF 0x3f3f3f
using namespace std;

int dist[DIM];
int viz[DIM];

vector < pair < int, int > > v[DIM];
priority_queue < pair < int, int > > Q;


int main()
{
    freopen("dijkstra.in","r",stdin);
    freopen("dijkstra.out","w",stdout);

    int n, m, i, j, x, y, c;

    scanf("%d%d",&n,&m);
    for( i = 1; i <= n; ++i ){
        scanf("%d%d%d",&x,&y,&c);
        v[x].push_back({y,c});
    }

    for( i = 2; i <= n; ++i ) dist[i] = INF;


    Q.push({1,0});
    while( !Q.empty() ){
        x = Q.top().first;
        Q.pop();

        if( viz[x] ) continue;
        else viz[x] = true;

        for( i = 0; i < v[x].size(); i++ ){
            y = v[x][i].first;  c = v[x][i].second;
            if( dist[y] > dist[x] + c ){
                dist[y] = dist[x] + c;
                Q.push({y,-dist[y]});
            }
        }
    }

    for(  i = 2; i <= n; ++i ){
        if( dist[i] != INF ) printf("%d ",dist[i]);
        else printf("0 ");
    }

    return 0;

}