Cod sursa(job #614110)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 5 octombrie 2011 17:57:04
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <queue>
#include <vector>
#include <string.h>

#define INF 0x3f3f3f3f
#define max_n 50001
#define max_m 250001
#define nod first
#define cost second

using namespace std;

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

int main ()
{
    int n,m,i,d[max_n],x,y,c,cst;
vector < pair < int, int > > g[max_n];
vector < pair < int, int > >::iterator it;
queue <int> q;
    in >> n >> m;
    for (i=1; i<=m; i++)
    {
        in >> x >> y >> cst;
        g[x].push_back(make_pair(y,cst));
    }

    memset(d,INF,sizeof(d));
    d[1]=0;
    q.push(1);
    while (!q.empty())
    {
        x=q.front();
        q.pop();
        for (it=g[x].begin(); it!=g[x].end(); it++)
        {
            y=(*it).nod;
            c=(*it).cost;
            if (d[y]>d[x]+c)
            {
                d[y]=d[x]+c;
                q.push(y);
            }
        }
    }
    for (i=2; i<=n; i++)
    if (d[i]!=INF)
         out << d[i] << ' ';
         else
         out << '0' << ' ';
    out << '\n';
    return 0;
}