Pagini recente » Cod sursa (job #1684464) | Profil silviu2233 | Statistici Calin Cristian Chirila (thewhitetiger) | Monitorul de evaluare | Cod sursa (job #1956052)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
const int NMax = 5e4 + 50;
const int inf = 2e9 + 5;
int N,M;
int dist[NMax];
struct muchie {
int y,c;
muchie(int _y = 0,int _c = 0) {
y = _y;
c = _c;
}
};
vector<muchie> v[NMax];
int main() {
in>>N>>M;
for (int i=1;i<=M;++i) {
int x,y,c;
in>>x>>y>>c;
v[x].push_back(muchie(y,c));
}
for (int i=2;i<=N;++i) {
dist[i] = inf;
}
bool ok = false;
for (int c=1;c<=N;++c) {
ok = false;
for (int i=1;i<=N;++i) {
for (int k=0;k < (int)v[i].size();++k) {
int next = v[i][k].y, cost = v[i][k].c;
if (dist[next] > dist[i] + cost) {
dist[next] = dist[i] + cost;
ok = true;
}
}
}
}
if (ok) {
out<<"Ciclu negativ!";
}
else {
for (int i=2;i<=N;++i) {
out<<dist[i]<<' ';
}
}
in.close();out.close();
return 0;
}