Pagini recente » Cod sursa (job #1280070) | Cod sursa (job #2999691) | Cod sursa (job #1997710) | Cod sursa (job #2982332) | Cod sursa (job #2209393)
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdlib>
#define INFINITE 1e9
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m, checked[50001], path[50001], x, y, c;
vector < pair <int , int> > v[50001];
queue <int> q;
void BellmanFord(int nod)
{
path[nod]=0;
q.push(nod);
while(!q.empty())
{
nod=q.front();
q.pop();
checked[nod]++;
if(checked[nod]>n)
{
out<<"Ciclu negativ!";
exit(0);
}
for(register int i=0; i<v[nod].size(); ++i)
{
if( path[nod]+v[nod][i].second < path[v[nod][i].first] )
{
path[v[nod][i].first] = path[nod]+v[nod][i].second;
q.push(v[nod][i].first);
}
}
}
}
int main()
{
in>>n>>m;
for(register int i=1; i<=m; ++i)
{
in>>x>>y>>c;
v[x].push_back( { y , c } );
}
fill(path+1, path+n+1, INFINITE);
BellmanFord(1);
for(register int i=2; i<=n; ++i)
out<<path[i]<<" ";
return 0;
}