Pagini recente » Cod sursa (job #2410466) | Cod sursa (job #1623572) | Cod sursa (job #251320) | Cod sursa (job #2185624) | Cod sursa (job #1894268)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bf.in");
ofstream fout("bf.out");
#define MAX 50010
#define inf 1e9
vector <pair<int, int> > G[MAX];
queue <int> Q;
int dist[MAX], ap[MAX];
int main()
{
int n, m, x, y, c, i, cm, nod;
fin >> n >> m;
cm = m;
while(m--)
{
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
dist[1] = 0;
for(i = 2 ; i <= n ; i++)
{
dist[i] = inf;
}
Q.push(1);
while(Q.size())
{
nod = Q.front();
Q.pop();
ap[nod]++;
if(ap[nod] > cm)
{
fout << "Ciclu negativ!\n";
return 0;
}
for(int it=0; it<G[nod].size(); it++)
{
if(dist[it.first] + nod < dist[it.first])
{
dist[it.first] = dist[it.first] + it.second;
Q.push(it.first);
}
}
}
for(i = 2 ; i <= n ; i++)
{
fout << dist[i] << ' ';
}
fout << "\n";
}