Cod sursa(job #1480592)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 2 septembrie 2015 20:56:36
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<iostream>
#include<fstream>
#include<queue>

using namespace std;

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


int n,m,cost[50005];

vector <int> L[50005];
queue <int> q;


void Citire()
{
  int x,y,costt;
  fin >> n >> m;
  for (int i=1; i<=m; i++)
   {
      fin >> x >> y >> costt;
      L[x].push_back(y);
	  L[x].push_back(costt);
   }
}

void Lee_Bellman_Ford_BFS_Dijkstra()
{
  int i,j,k,costt;
  q.push(1); // in coada stau noduri
  cost[1]=0;
  
  while (!q.empty())
 {
   k = q.front();
   q.pop();
   
   for (int j=0; j<L[k].size(); j=j+2)
       {
            i = L[k][j]; costt = L[k][j+1];
            
            if (!cost[i] || cost[i] >= cost[k] + costt)
               {
                   cost[i] = cost[k] + costt;
                   q.push(i);
               }
       }
 }  
}

void Print_that()
{
 for (int i=2; i<=n; i++)
   fout << cost[i] << " ";
 fout << "\n";
}

int main ()
{
 Citire();
 Lee_Bellman_Ford_BFS_Dijkstra();
 Print_that();
 fin.close();
 fout.close();
 return 0;
}