Pagini recente » Cod sursa (job #615535) | Cod sursa (job #2074382) | Cod sursa (job #1152647) | Cod sursa (job #1609427) | Cod sursa (job #1587008)
#include <bits/stdc++.h>
#define N 50001
#define M 250001
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct nod
{
int ind, cost;
nod *next;
}*graf[N];
int n, m, d[N], Q[M];
void add(int x, int y, int c)
{
if(graf[x] == NULL)
{
nod *p = new nod;
p -> ind = x;
p -> cost = 0;
p -> next = NULL;
graf[x] = p;
}
nod *nou = new nod, *p = graf[x];
nou -> ind = y;
nou -> cost = c;
nou -> next = NULL;
while(p -> next != NULL)
p = p -> next;
p -> next = nou;
}
void Read()
{
int x, y, c;
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> x >> y >> c;
add(x, y, c);
}
}
void Inf_vec()
{
for(int i = 2; i <= n; i++)
d[i] = INFINITY;
}
void Print()
{
for(int i = 2; i <= n; i++)
{
fout << d[i] << " ";
}
}
void Dijkstra()
{
int prim = 0, ultim = 1, curent;
Q[0] = 1;
while(prim <= ultim)
{
curent = Q[prim++];
while(graf[curent] != NULL)
{
if(graf[curent] -> cost + d[curent] < d[graf[curent] -> ind])
{
d[graf[curent] -> ind] = graf[curent] -> cost + d[curent];
Q[ultim++] = graf[curent] -> ind;
}
graf[curent] = graf[curent] -> next;
}
}
}
int main()
{
Read();
Inf_vec();
Dijkstra();
Print();
return 0;
}