Pagini recente » Cod sursa (job #1742125) | Cod sursa (job #2611410)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
const int NMAX = 100005;
const int MMAX = 250005;
const int INF = 1e9;
int n, m, dist[NMAX];
struct muchie {
int x, y, c;
};
muchie a[MMAX];
int main()
{
f >> n >> m;
for(int i = 1; i <= m; i++) {
f >> a[i].x >> a[i].y >> a[i].c;
}
for(int i = 1; i <= n; i++) {
dist[i] = INF;
}
dist[1] = 0;
for(int i = 1; i <= n - 1; i++) {
for(int j = 1; j <= m; j++) {
if(dist[a[j].x] + a[j].c < dist[a[j].y]) {
dist[a[j].y] = dist[a[j].x] + a[j].c;
}
}
}
for(int i = 1; i <= m; i++) {
if(dist[a[i].x] + a[i].c < dist[a[i].y]) {
g << "Ciclu negativ!";
return 0;
}
}
for(int i = 2; i <= n; i++) {
g << dist[i] << ' ';
}
return 0;
}