Pagini recente » Borderou de evaluare (job #1259280) | Borderou de evaluare (job #1184610) | Borderou de evaluare (job #1781789) | Borderou de evaluare (job #861463) | Cod sursa (job #1988864)
#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 << "Cicle 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;
}