Pagini recente » Cod sursa (job #2357438) | Cod sursa (job #730051) | Cod sursa (job #3148385) | Cod sursa (job #898037) | Cod sursa (job #2932698)
#include <iostream>
#include <fstream>
#define inf 2147483647
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int a[3002][3002];
int aux, aux2, c;
int n, m;
int distante[3002];
void citire()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> aux >> aux2 >> c;
a[aux][aux2] = c;
if(aux == 1) distante[aux2] = c;
}
for(int i = 2; i <= n; i++)
{
if(distante[i] == 0)
distante[i] = inf;
}
}
void dijkstra()
{
bool ok = true;
while(ok)
{
ok = false;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(distante[i] + a[i][j] < distante[j] && a[i][j])
{
distante[j] = distante[i] + a[i][j];
ok = true;
}
}
}
}
}
int main()
{
citire();
dijkstra();
for(int i = 2; i <= n; i++)
{
if(distante[i] != inf)
fout << distante[i] << ' ';
else
fout << 0 <<' ';
}
return 0;
}