Pagini recente » Cod sursa (job #2705268) | Cod sursa (job #990668) | Cod sursa (job #600741) | Cod sursa (job #2907371) | Cod sursa (job #3004790)
#include <fstream>
using namespace std;
const int N = 102;
const int lim = 1e9;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
bool fq[N];
int d[N], n, mat[N][N];
void dij(int x)
{
//out << x << endl;
fq[x] = true;
int mini = 0;
for (int i = 1; i <= n; i++)
{
d[i] = min(d[i], d[x] + mat[x][i]);
}
for (int i = 1; i <= n; i++)
if (d[mini] >= d[i] && !fq[i])
mini = i;
if (d[mini] != lim)
dij(mini);
}
int main()
{
int m, ns, nd, ct;
in >> n >> m;
d[0] = lim;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
mat[i][j] = lim;
d[i] = lim;
}
d[1] = 0;
while (in >> ns >> nd >> ct)
{
mat[ns][nd] = ct;
}
dij(1);
for (int i = 2; i <= n; i++)
{
if (d[i] == lim)
d[i] = 0;
out << d[i] << " ";
}
return 0;
}