Cod sursa(job #1413775)

Utilizator pulseOvidiu Giorgi pulse Data 2 aprilie 2015 08:31:32
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const int maxn = 50010;
const int inf = 2e9;

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

int n, m, d[maxn];
vector <pair <int, int> > g[maxn];
queue <int> q;
bool inq[maxn];

void dijkstra()
{
    for (int i = 2; i <= n; i++)
        d[i] = inf;
    q.push(1);
    while (!q.empty())
    {
        int node = q.front();
        q.pop();
        inq[node] = 0;
        for (vector <pair <int, int> > :: iterator it = g[node].begin(); it != g[node].end(); ++it)
        {
            int v = it -> first;
            int c = it -> second;
            if (d[v] > d[node] + c)
            {
                d[v] = d[node] + c;
                if (!inq[v])
                {
                    inq[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    for (int i = 2; i <= n; i++)
    {
        if (d[i] != inf) fout << d[i] << " ";
        else fout << "0 ";
    }
}

int main()
{
    fin >> n >> m;
    for (int a, b, c; m; --m)
    {
        fin >> a >> b >> c;
        g[a].push_back(make_pair(b, c));
    }
    dijkstra();
    return 0;
}