Pagini recente » Cod sursa (job #2031518) | Cod sursa (job #2491583) | Cod sursa (job #2532665) | Cod sursa (job #1476207) | Cod sursa (job #2423279)
#include <iostream>
#include <fstream>
#define inf 20010
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct graf{
int nod, cost;
graf *next;
};
graf *a[110], *t;
int n, m, d[110], q[110], x, y, c;
void dijkstra()
{
for ( int i = 2; i <= n; ++i )
d[i] = inf;
int min, pmin = 0;
for ( int i = 1; i <= n; ++i )
{
min = inf;
for ( int j = 1; j <= n; ++j )
if ( d[j] < min && !q[j] )
min = d[j], pmin = j;
q[pmin] = 1;
graf *t = a[pmin];
while ( t )
{
if ( d[ t->nod ] > d[pmin] + t->cost )
d[ t->nod ] = d[pmin] + t->cost;
t = t->next;
}
}
}
int main()
{
int i;
fin >> n >> m;
for(i=1; i<=n; i++)
{
fin >> x >> y >> c;
t = new graf;
t->nod = x;
t->cost = c;
t->next = a[y];
a[y] = t;
t = new graf;
t->nod = y;
t->cost = c;
t->next = a[x];
a[x] = t;
}
dijkstra();
for(i=2; i<=n; i++)
{
fout << d[i] << " ";
}
return 0;
}