Pagini recente » Cod sursa (job #2722380) | Cod sursa (job #1917884) | Cod sursa (job #1488632) | Cod sursa (job #382959) | Cod sursa (job #658709)
Cod sursa(job #658709)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define maxN 250005
#define INF 0x3f3f3f3f
vector < pair <int , int> > lista[maxN];
queue <int> Q;
long long cost[maxN] , cost_aux;
bool viz[maxN];
int N , M;
int main ()
{
freopen ("dijkstra.in" , "r" , stdin);
freopen ("dijkstra.out" , "w" , stdout);
scanf ("%d %d" , &N , &M);
int a , b , c;
for (int i = 1 ; i <= M ; ++i)
{
scanf ("%d %d %d" , &a , &b , &c);
lista[a].push_back (make_pair (b , c));
}
for (int i = 1 ; i <= N ; ++i)
cost[i] = INF;
int nod;
Q.push (1);
cost[1] = 0;
while (!Q.empty ())
{
nod = Q.front ();
for (unsigned i = 0 ; i < lista[nod].size () ; ++i)
{
/*if (viz[lista[nod][i].first])
continue;*/
cost_aux = cost[nod] + lista[nod][i].second;
if (cost[lista[nod][i].first] == INF)
cost[lista[nod][i].first] = cost_aux;
else
if (cost[lista[nod][i].first] > cost_aux)
cost[lista[nod][i].first] = cost_aux;
//viz[lista[nod][i].first] = 1;
Q.push (lista[nod][i].first);
}
Q.pop ();
}
for (int i = 2 ; i <= N ; ++i)
if (cost[i] != INF)
printf ("%lld " , cost[i]);
else printf ("0 ");
return 0;
}