Cod sursa(job #1131486)

Utilizator VisuianMihaiMihai Visuian VisuianMihai Data 28 februarie 2014 20:29:23
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

vector<pair<int,int> >g[50001];
queue<int>q;
int c[50001], n, m, x, y, z;

void Dijkstra(int x)
{
    q.push(x);
    for(int i = 2; i<= n; i++ )
        c[i]=999999999;
    while(!q.empty())
    {
        x=q.front(); q.pop();
        for(vector<pair<int,int> >::iterator it=g[x].begin(); it<g[x].end(); it++ )
        {
            if(c[it->first]>c[x]+it->second)
            {
                c[it->first]=c[x]+it->second;
                q.push(it->first);
            }
        }
    }
}

int main()
{
    fin>>n>>m;
    for(int i = 1; i<= m; i++ )
    {
        fin>>x>>y>>z;
        g[x].push_back(make_pair(y,z));
    }
    Dijkstra(1);
    for(int i = 2; i<= n; i++ )
        fout<<c[i]<<' ';
    fin.close();
    fout.close();
    return 0;
}