Cod sursa(job #1919761)

Utilizator GeoeyMexicanuBadita George GeoeyMexicanu Data 9 martie 2017 20:58:23
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#define N 50010
#define INF 200000010

using namespace std;

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

vector< pair<int,int> > vec[N];
set<pair<int,int> > h;
int i,j,n,m,k,t,p,r,dist[N];

int main()
{
    int x,y,z,to,cost;
    f>>n>>m;
    while(m!=0)
    {
        f>>x>>y>>z;
        vec[x].push_back(make_pair(y,z));
        m--;
    }
    for(i=1;i<=n;i++)
        dist[i]=INF;
    h.insert(make_pair(0,1));
    dist[1]=0;
    int nod;
    while(!h.empty())
    {
        nod=h.begin()->second;
        h.erase(h.begin());
        for(vector< pair<int,int> >::iterator it=vec[nod].begin();it!=vec[nod].end();it++)
        {
            to=it->first;
            cost=it->second;
            if(dist[to]>dist[nod]+cost)
            {
                if(dist[to]!=INF)
                {
                    h.erase(h.find(make_pair(dist[to],to)));
                }
                dist[to]=dist[nod]+cost;
                h.insert(make_pair(dist[to],to));
            }
        }
    }
    for(i=2;i<=n;i++)
    if(dist[i]==INF)
    g<<0<<' ';
    else
        g<<dist[i]<<' ';
}