Pagini recente » Cod sursa (job #2243954) | Cod sursa (job #418372) | Cod sursa (job #413208) | Cod sursa (job #2526310) | Cod sursa (job #700284)
Cod sursa(job #700284)
// O(N ^ 2)
// MUIE ACTA
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define PB push_back
#define MKP make_pair
#define INF 0x3f3f3f3f
#define maxN 50005
int N , M , cost[maxN];
bool viz[maxN];
vector <pair <int , int> > lista[maxN];
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].PB (MKP (b , c));
}
for (int i = 2 ; i <= N ; ++i)
cost[i] = INF;
int aux = N;
while (aux)
{
int minim = INF , nod;
--aux;
for (int i = 1 ; i <= N ; ++i)
if (cost[i] < minim && !viz[i])
{
minim = cost[i];
nod = i;
}
viz[nod] = true;
for (unsigned int i = 0 ; i < lista[nod].size () ; ++i)
{
int nodcur = lista[nod][i].first;
int costcur = lista[nod][i].second;
if (viz[nodcur]) continue;
if (cost[nodcur] <= cost[nod] + costcur) continue;
cost[nodcur] = cost[nod] + costcur;
}
}
for (int i = 2 ; i <= N ; ++i)
if (cost[i] == INF)
printf ("0 ");
else printf ("%d " , cost[i]);
return 0;
}