Cod sursa(job #2816599)

Utilizator VladOSBVlad Oleksik VladOSB Data 11 decembrie 2021 18:38:52
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <vector>
#define oo 1000000

using namespace std;

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

//int h[103][103];
vector <pair <int, int> > h[103];
int t[103],d[103];
bool v[103];

int main()
{
    int n,m=0,i,j,c,cmin,p,k,x,l;
    in >> n >> x;
    /*for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            h[i][j]=oo;
            if(i==j)
                h[i][j]=0;
        }
    }*/
    while(in >> i >> j >> c)
    {
        m++;
        //h[i][j]=c;
        h[i].push_back({j,c});
    }
    for(i=1;i<=n;i++)
    {
        d[i]=oo;
        t[i]=0;
        v[i]=0;
    }
    d[x]=0;
    for(i=0;i<n-1;i++)
    {
        cmin=oo;
        for(j=1;j<=n;j++)
        {
            if(d[j]<cmin && !v[j])
            {
                cmin=d[j];
                p=j;
            }
        }
        v[p]=1;
        l=h[p].size();
        //for(k=0;k<l;k++)
        /*{
            if(d[h[p][k].first]>d[p]+h[p][k].second)
            {
                d[h[p][k].first]=d[p]+h[p][k].second;
                t[h[p][k].first]=p;
            }
        }*/
        for(pair <int, int> k:h[p])
        {
            if(d[k.first]>d[p]+k.second)
            {
                d[k.first]=d[p]+k.second;
                t[k.first]=p;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        if(d[i]<oo)
            out << d[i] << ' ';
        else
            out << 0 << ' ';
    }
}