Cod sursa(job #2176045)

Utilizator pigwingsAntohi Vasile pigwings Data 16 martie 2018 20:35:22
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>
#define nn 50003
#define  mm 999999999
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector<pair<int,int> >a[nn];
queue<pair<int,int> >q;
int dist[nn],x,y,z,n,m;
void dij(int k)
{
    dist[k]=0;
    q.push(make_pair(0,k));
    while(!q.empty())
    {
        int nod =q.front().second;
        q.pop();
        vector<pair<int,int> >::iterator it;
        for(it=a[nod].begin();it!=a[nod].end();it++)
            if(dist[it->first]>dist[nod]+it->second)
        {
            dist[it->first]=dist[nod]+it->second;
            q.push(make_pair(dist[it->first],it->first));

        }
    }
}
int main()
{
    f>>n>>m;
    for(int i =1; i <= m; ++i)
    {
         f >> x >> y >> z;
         a[x].push_back(make_pair(y, z));
    }
    for(int i=1;i<=n;i++)
        dist[i]=mm;
        dij(1);
        for(int i = 2; i <= n; ++i)
        if(dist[i] == mm) g << 0 << " ";
        else g << dist[i] << " ";
}