Cod sursa(job #2165870)

Utilizator MaaaaaCristina Ma Maaaaa Data 13 martie 2018 14:07:55
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>
#include <vector>
#include <queue>
#define nmax 50005
#define mmax 250005
#define inf 200000000000
using namespace std;
fstream f1("dijkstra.in", ios::in);
fstream f2("dijkstra.out", ios::out);
long long dist[nmax];
int n, m, viz[nmax];
vector <pair<int, int> > v[nmax];
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;
void citire()
{
   int i, x, y, c;
   f1>>n>>m;
   for(i=1; i<=m; i++)
   {
       f1>>x>>y>>c;
       v[x].push_back({y, c});
   }
   dist[1]=0;
   for(i=2; i<=n; i++) dist[i]=inf;
   /*for(auto it=v[1].begin(); it!=v[1].end(); ++it)
       dist[(*it).first]=(*it).second;*/
   q.push({0, 1});
}
void dijk()
{
    int nod, dis;
    while(!q.empty())
    {
        dis=q.top().first; nod=q.top().second;
        q.pop();
        viz[nod]=1;
        if(dis== dist[nod])
        {
            for(auto it=v[nod].begin(); it!=v[nod].end(); ++it)
                if((!viz[(*it).first])&&(dist[(*it).first]> dis+(*it).second))
            {
                dist[(*it).first]= dis+(*it).second;
                q.push({dist[(*it).first], (*it).first});
            }
        }
    }
}
void afis()
{
    int  i;
    for(i=2; i<=n; i++)
        if(dist[i]!=inf) f2<<dist[i]<<' ';
        else f2<<0<<' ';
}
int main()
{
    citire();
    dijk();
    afis();
    return 0;
}