Pagini recente » Cod sursa (job #2906675) | Cod sursa (job #2249899) | Cod sursa (job #1784572) | Cod sursa (job #2829335) | Cod sursa (job #2735130)
#include <iostream>
#include <fstream>
using namespace std;
const int N = 1e5 + 3;
const int inf = 2e9;
struct Nod
{
int x, c;
Nod * next;
};
Nod * g[N];
int d[N];
bool viz[N];
void addNod(int a, int x, int c)
{
Nod *tmp = new Nod;
tmp->x = x;
tmp->c = c;
tmp->next = NULL;
if(g[a] == NULL)
{
g[a] = tmp;
}
else
{
Nod *t = g[a];
while(t->next != NULL)
t = t->next;
t->next = tmp;
}
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
for(int i=0;i<N;i++)
d[i] = inf;
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a, b, c;
cin>>a>>b>>c;
addNod(a, b, c);
}
d[1] = 0;
for(int i=1;i<=n;i++)
{
int v = -1;
for(int j=1;j<=n;j++)
{
if(!viz[j] && (v == -1 || d[j] < d[v]))
v = j;
}
if(d[v] == inf)
break;
viz[v] = 1;
Nod *t = g[v];
while(t != NULL)
{
if(d[v] + t->c < d[t->x])
{
d[t->x] = d[v] + t->c;
}
t = t->next;
}
}
for(int i=2;i<=n;i++)
cout<<d[i]<<" ";
return 0;
}