Cod sursa(job #2165801)

Utilizator MaaaaaCristina Ma Maaaaa Data 13 martie 2018 13:43:07
Problema Algoritmul lui Dijkstra Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>
#include <vector>
#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];
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;
   viz[1]=1;
}
void dijk()
{
    int i, j, vfmin;
    long long mini;
    for(i=1; i<n; i++)
    {
        mini=inf; vfmin=0;
        for(j=1; j<=n; j++)
            if((!viz[j])&&(dist[j]< mini)) {mini=dist[j]; vfmin=j;}
        viz[vfmin]=1;
        for(auto it=v[vfmin].begin(); it!=v[vfmin].end(); ++it)
            if(dist[(*it).first]> dist[vfmin]+(*it).second)
               dist[(*it).first]= dist[vfmin]+(*it).second;
    }
}
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;
}