Pagini recente » Cod sursa (job #2416801) | info-arena 2.0 | Borderou de evaluare (job #804959) | Cod sursa (job #3154210) | Cod sursa (job #1988865)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
const int maxN = 5e4+5;
vector<pair<int,int>> g[maxN];
queue<int> coada;
int dist[maxN], f[maxN];
int main()
{
int n, m;
fin >> n >> m;
for(int i=1; i<=m; i++)
{
int x, y, z;
fin >> x >> y >> z;
g[x].push_back({y,z});
}
for(int i =1; i<=n; i++)
dist[i] = INT_MAX;
dist[1] = 0;
coada.push(1);
while(!coada.empty())
{
int nod = coada.front();
coada.pop();
if(++f[nod] > m)
{
fout << "Ciclu negativ!\n";
return 0;
}
for(auto it: g[nod]){
if(dist[nod] + it.second < dist[it.first])
{
dist[it.first] = dist[nod] + it.second;
coada.push(it.first);
}
}
}
for(int i=2; i<=n; i++)
fout << dist[i] << " ";
return 0;
}