Cod sursa(job #2103498)

Utilizator eduardandrei20Nechifor Eduard Andrei eduardandrei20 Data 10 ianuarie 2018 13:26:51
Problema Algoritmul lui Dijkstra Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.21 kb
#include <bits/stdc++.h>
// edy face belman cu coada !
#define NN 50010
#define dim 10000
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
char buff[dim];
int poz = 0;
void citire( int & nr )
{
    nr =0;
       while (buff[poz]<'0' || buff[poz]>'9')
                if(++poz>dim)fread(buff,1,dim,stdin),poz=0;
       while(buff[poz]>='0'&&buff[poz]<='9')
       {
          nr=nr*10+buff[poz]-'0';
           if(++poz>dim)fread(buff,1,dim,stdin),poz=0;
       }
}



int n , m ;
// noduri si muchii
 int costuri [ NN ];
struct nod{
int info;
int COST ;
nod *next;}*L[NN];

queue<int>q ;
// asta i coada
bool used[NN];
 // nodurile folosite

 void add (int x , int y , int cost )
 {
      // adaug pe y la lista lui x
      nod *aux = new nod ;
      // aloc mem
      aux -> info = y ;
      aux ->COST = cost;
      aux -> next = L[x];
      L[x]=aux;
 }

 void hai_sa_citesc()
 {
     freopen("dijkstra.in","r",stdin);
      citire(n); citire(m);
         for ( int i = 1 ; i <= m ; ++ i )
         {
             int x, y ,cost;
             citire(x); citire(y); citire(cost);
             //in >> x >> y>>cost ;
             add(x,y,cost);
         }
 }



int main()
{ int infi= INT_MAX;
      hai_sa_citesc();
      q.push(1);
    used[1] = true ;
    costuri[1]=0;
      //pai de la 1 incep !
        for ( int i = 2; i <= n ; ++ i )
                costuri[i] = infi;
          // am pus si costurile aici
          //asa s la inceput
          // acum belman


          while( !q.empty() )
        {   int node = q.front();
        q.pop();
         used[node]= false;
            for ( nod *aux = L[node] ; aux ; aux = aux ->next)
            {
                if( costuri[aux->info]>costuri[node]+aux->COST)
                      {costuri[aux->info] = costuri[node]+aux->COST;


                if (!used[aux->info])
                {
                    q.push(aux->info);
                    used[aux->info] = true ;
                }
               }
            }
        }

for ( int i = 2; i <= n ; ++ i )
     if(costuri[i] !=infi)
       out << costuri[i]<<" ";
else out << 0<<" ";



    return  0 ;
}