Cod sursa(job #2361147)

Utilizator pinbuAdi Giri pinbu Data 2 martie 2019 13:23:17
Problema Algoritmul lui Dijkstra Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
#define N 101
#define INF 100001
using namespace std;

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

vector<pair<int,int>> v[N];
queue<int> q;
int n,m,nod,d[N];
bool viz[N];

//pereche de tipul v[nod] = {nod,cost}
int main()
{
    int i,a,b,c;
    
    fin>>n>>m;
    
    while(fin>>a>>b>>c)
    {
        v[a].push_back(make_pair(b,c));
        //v[b].push_back(make_pair(a,c));
    }
    
    for(i=1;i<=n;i++)
        d[i]=INF;
    d[1]=0;
    q.push(1);
    
    while(!q.empty())
    {
        int vf = q.front();
        q.pop();
        
        if(!viz[vf])
        {
            viz[vf]=true;
            
            for(auto s:v[vf])
            {
                if(d[vf]+s.second<d[s.first])
                {
                    d[s.first]=d[vf]+s.second;
                    q.push(s.first);
                    viz[s.first]=false;
                }
            }
        }
    }
    
    for(i=2;i<=n;i++)
        if(d[i]==INF)
            fout<<"0 ";
        else
            fout<<d[i]<<" ";
    return 0;
    
}