Cod sursa(job #2424061)

Utilizator alexandradonici99@yahoo.comAlexandra Donici [email protected] Data 22 mai 2019 16:02:22
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include <climits>
#include <fstream>
#include <list>
#include <vector>
#include <set>

using namespace std;
#define inf INT_MAX/2

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");


void citire(list <pair <int,int> > * &L,int &n,int &m)
{
    fin>>n>>m;
    L=new list<pair<int,int> >[n+1];
    int x,y,p;
    for(int i=0;i<m;i++)
    {
        fin>>x>>y>>p;
        L[x].push_back({p,y});
        //L[y].push_back({p,x});
    }

}

void initializare(vector <int> &t,vector <int> &d, int n,int st)
{
    t.resize(n+1);
    d.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        t[i]=0;
        d[i]=inf;

    }

    d[st]=0;

}

void Dijkstra(list <pair <int,int> > *L,int n,int m,int st,vector <int> &t,vector <int> &d)
{

    set <pair <int,int> > Q;
    initializare(t,d,n,st);

    Q.insert(make_pair(0,1));
    while(!Q.empty())
    {
        int u=(*Q.begin()).second;
        Q.erase(Q.begin());
        for(auto v:L[u])
        {
           int y=v.second;
            int p=v.first;
            if(d[y]>d[u]+p)
            {
                Q.erase(make_pair(d[y],y));
                d[y]=d[u]+p;
                Q.insert(make_pair(d[y],y));
            }
        }
    }
    for(int i=2;i<=n;i++)
    {
        fout<<d[i]<<' ';
    }
    fout<<'\n';

}
int main()
{
    list <pair <int,int> > *L;
    int n,m,st;
    vector <int> t;
    vector <int> d;
    citire(L,n,m);

    Dijkstra(L,n,m,1,t,d);
    return 0;
}