Pagini recente » Cod sursa (job #1799383) | Cod sursa (job #111475) | Cod sursa (job #212421) | Cod sursa (job #2099375) | Cod sursa (job #2091946)
#include <bits/stdc++.h>
// edy face belman cu coada !
#define NN 50010
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
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()
{
in >> n >> m ;
for ( int i = 1 ; i <= m ; ++ i )
{
int x, y ,cost;
in >> x >> y>>cost ;
add(x,y,cost);
}
}
int main()
{ int infi= INT_MAX;
hai_sa_citesc();
q.push(1);
used[1] = true ;
//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 from = q.front() ;
q.pop();
for ( nod * aux = L[from] ; aux ; aux = aux ->next)
{ int to = aux ->info;
if(costuri[to] > costuri[from] + aux->COST )
{
costuri [to] = costuri[from] + aux->COST;
if(!used[to])
{
q.push(to);
used[to]= true;
}
}
}
}
for ( int i = 2; i <= n ; ++ i )
if(costuri[i] !=infi)
out << costuri[i]<<" ";
else out << 0;
return 0 ;
}