Cod sursa(job #1710521)

Utilizator mihaidanielmihai daniel mihaidaniel Data 29 mai 2016 10:13:25
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

vector < pair<int,int> >v[50001];
priority_queue <int>q;
bool w[50001];
int dist[50001];

int main()
{
    freopen( "dijkstra.in", "r", stdin );
    freopen( "dijkstra.out", "w", stdout );
    int n, m, i, x, y, z;
    scanf( "%d%d", &n, &m );
    for( i=2; i<=n; ++i )dist[i]=0x3f3f3f3f;
    for( i=1; i<=m; ++i )
    {
        scanf( "%d%d%d", &x, &y, &z );
        v[x].push_back( {y,z} );
    }
    q.push(1);
    while( !q.empty() )
    {
        x=q.top();q.pop();w[x]=0;
        for( auto vec : v[x] )
            if( dist[vec.first]>dist[x]+vec.second )
            {
                dist[vec.first]=dist[x]+vec.second;
                if( !w[vec.first] )
                {
                    q.push(vec.first);
                    w[x]=1;
                }
            }
    }
    for( i=2; i<=n; ++i )
        if( dist[i]==0x3f3f3f3f )printf("0 ");
        else printf( "%d ", dist[i] );
    return 0;
}