Cod sursa(job #1235302)

Utilizator TeodorPPaius Teodor TeodorP Data 29 septembrie 2014 14:43:09
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <vector>
#include <queue>
#define Inf 0x3f3f3f
using namespace std;

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

struct Edge{
    int v;
    int cost;
};

int n, m;
vector<vector<Edge> >a;
queue<int> q;
int x, y, c;
Edge aux;
int d[55000];
bool ok[55000];


int main()
{
    is >> n >> m;
    a.resize(m+1);
    for(int i = 1; i <= m; ++i)
    {
        is >> x;
        is >> aux.v;
        is >> aux.cost;
        a[x].push_back(aux);
    }
    d[1] = 0;
    for(int i = 2; i <= n; ++i)
        d[i] = Inf;

    q.push(1);
    ok[1] = true;

    while(!q.empty())
    {
        x = q.front();
        q.pop();
        ok[x] = false;
        for(vector<Edge>::iterator it = a[x].begin(); it != a[x].end(); ++it)
        {
            y = it->v;
            c = it->cost;
            if(d[y] > d[x] + c)
            {
                d[y] = d[x] + c;
                if(!ok[y])
                {
                    q.push(y);
                    ok[y] = true;
                }
            }
        }

    }

    for(int i = 2; i <= n; ++i)
        os << d[i] << ' ';
    is.close();
    os.close();
    return 0;
}