Pagini recente » Borderou de evaluare (job #2975937) | Borderou de evaluare (job #1510081) | Borderou de evaluare (job #2617875) | Borderou de evaluare (job #1253849) | Cod sursa (job #1988763)
#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];
priority_queue<pair<int,int>> coada;
unordered_map<int,bool> myHash;
int dist[maxN];
int main()
{
int n, m;
fin >> n >> m;
while(m--)
{
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;
myHash[1] = true;
for(int k=1; k<=n; k++)
{
for(int i=1; i<=n; i++)
{
if(myHash[i])
if(dist[i] != INT_MAX)
{
myHash[i] = false;
for(auto it: g[i])
if(dist[i] + it.second < dist[it.first]){
dist[it.first] = dist[i] + it.second;
myHash[it.first] = true;
}
}
}
}
for(int i=1; i<=n; i++)
{
if(dist[i] != INT_MAX)
{
for(auto it: g[i])
if(dist[i] + it.second < dist[it.first])
{
fout << "Ciclu negativ!\n";
return 0;
}
}
}
for(int i=2; i<=n; i++)
fout << dist[i] << " ";
return 0;
}