Pagini recente » Cod sursa (job #2049853) | Cod sursa (job #363054) | Cod sursa (job #1514993) | Statistici Ana-Maria Kupas-Spunei (anak) | Cod sursa (job #2203043)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m, mat[1005][1005], c[1000000], dist[1005];
int main()
{
in >> n >> m;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) mat[i][j] = -1;
for(int i= 1; i <= m; i++)
{
int x, y, z;
in >> x >> y >> z;
mat[x][y] = z;
}
c[1] = 1;
int s = 0, ct = 1;
for(int i = 1; i <= n; i++)
dist[i] = 999999999;
dist[1] = 0;
while(s != ct)
{
s++;
int x = c[s];
for(int i = 1; i <= n; i++)
if(mat[x][i] != -1 && dist[x] + mat[x][i] < dist[i])
{
ct++;
c[ct] = i;
dist[i] = dist[x] + mat[x][i];
}
}
for(int i = 2; i <= n; i++)
if(dist[i] != 999999999)
out << dist[i] << " ";
else out << 0 << " ";
return 0;
}