Cod sursa(job #1291409)

Utilizator marian98Horodnic Gheorghe Marian marian98 Data 12 decembrie 2014 19:19:33
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include<cstdio>
#include<queue>
#include<vector>
#include<fstream>
#define maxN 50001
#define M 1<<30
using namespace std;
struct nod{
    int inf,c;
};
int n,m,cost[maxN];
vector<nod>v[maxN];
queue<int>heap;

void citire()
{
    ifstream f("dijkstra.in");
    nod a;
    int i,x,y,z;
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>x>>y>>z;
        a.inf=y;
        a.c=z;
        v[x].push_back(a);
    }
    for(i=2;i<=n;i++)
        cost[i]=M;
}
void dijkstra()
{
    nod a;
    int i,nod_cur;
    heap.push(1);
    while(!heap.empty())
    {
        nod_cur=heap.front();
        heap.pop();
        for(i=0;i<v[nod_cur].size();i++)
        {
            if(cost[v[nod_cur][i].inf]>cost[nod_cur]+v[nod_cur][i].c)
            {
                cost[v[nod_cur][i].inf]=cost[nod_cur]+v[nod_cur][i].c ;
                heap.push(v[nod_cur][i].inf);
            }
        }
    }
}
void afisare()
{
    ofstream f1("dijkstra.out");
    int i;
    for(i=2;i<=n;i++)
    {
        if(cost[i]==M)f1<<"0 ";
        else f1<<cost[i]<<" ";
    }
}
int main()
{
    citire();
    dijkstra();
    afisare();
    return 0;
}