Cod sursa(job #1290188)

Utilizator marian98Horodnic Gheorghe Marian marian98 Data 10 decembrie 2014 21:56:01
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
unsigned long m,d[50001];
int main()
{
    vector<unsigned>A[50001];
    unsigned n,nod=1;
    ifstream f("dijkstra.in");
    ofstream f1("dijkstra.out");
    f>>n>>m;
    for (unsigned i=1;i<=n;i++)
        A[i].assign(n+1,9999);
    for (unsigned i=1;i<=m;i++)
    {
        unsigned a,b,c;
        f>>a>>b>>c;
        A[a][b]=c;
    }
    for (unsigned i=1;i<=n;i++)
        d[i]=9999;
    d[1]=0;
    while(nod<=n)
    {
        for (unsigned i=1;i<=n;i++)
            if (d[i]>d[nod]+A[nod][i]) d[i]=d[nod]+A[nod][i];
        nod++;
    }
    for (unsigned i=2;i<=n;i++)
        f1<<d[i]<<" ";
    return 0;
}