Cod sursa(job #1872169)

Utilizator dragomirmanuelDragomir Manuel dragomirmanuel Data 7 februarie 2017 23:28:01
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>

#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f

using namespace std;
const int MUCHII_MAX = 250005;
const int NODURI_MAX = 50005;

int N,M;
int dist[NODURI_MAX];

vector < pair < int, int > > G[MUCHII_MAX];
priority_queue < pair < int, int > > Q;

void Read()
{
    scanf("%d%d", &N, &M);

    for(int i=1; i<=M; ++i)
    {
        int x,y,c;
        scanf("%d%d%d",&x, &y, &c);
        G[x].pb(mp(y,c));
    }

    for(int i=2; i<=N; ++i)
        dist[i]=inf;
}

void Solve()
{
    Q.push(mp(0,1));

    while(!Q.empty())
    {
        int c, nod;
        c=-Q.top().first;
        nod=Q.top().second;
        Q.pop();

        vector < pair < int, int > > ::iterator it;

        for(it=G[nod].begin(); it!=G[nod].end(); ++it)
        {
            if(dist[it->first] > c+it->second)
            {
                dist[it->first]=c+it->second;
                Q.push(mp(-(dist[it->first]),it->first));
            }
        }
    }

    for(int i=2; i<=N; ++i)
        if(dist[i]==inf)
            cout<<0<<" ";
        else cout<<dist[i]<<" ";
}

int main()
{
    freopen("dijkstra.in", "r", stdin);
    freopen("dijkstra.out","w", stdout);
    Read();
    Solve();
    return 0;
}