Cod sursa(job #1413235)

Utilizator httpsLup Vasile https Data 1 aprilie 2015 19:02:19
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

#ifdef INFOARENA
ifstream f("dijkstra.in");
#define cout g
#else
ifstream f("date.in");
#endif // INFOARENA

ofstream g("dijkstra.out");

#define nmax 50001
#define inf 0x3f3f3f3f

vector<pair<int,int>> G[nmax];
int dist[nmax];
int n,m,x,z,y,i,nod,cost;

struct eee{int nod,cost;
bool operator <(eee a) const{return cost>a.cost;};
};

priority_queue<eee> heap;

int main()
{
    f>>n>>m;
    memset(dist,inf,sizeof dist);

    for(;m;--m)
    {
        f>>x>>y>>z;
        G[x].push_back({y,z});
    }

    dist[1]=0;
    heap.push({1,0});
    while(!heap.empty())
    {
        nod=heap.top().nod;
        cost=heap.top().cost;
        heap.pop();
        for(auto vec:G[nod])
            if(cost+vec.second<dist[vec.first])
        {
            dist[vec.first]=cost+vec.second;
            heap.push({vec.first,dist[vec.first]});
        }
    }
    for(i=2;i<=n;++i) cout<<(dist[i]!=inf ? dist[i]:0)<<' ';
    return 0;
}