Pagini recente » Cod sursa (job #1350844) | Cod sursa (job #2090816) | Cod sursa (job #594578) | Cod sursa (job #2956550) | Cod sursa (job #2262446)
#include <iostream>
#include <bits/stdc++.h>
#define inf 200000
using namespace std;
ifstream fin("bellman.in");
ofstream fout("bellman.out");
struct muchie{
int x,y,c;
};
int tata[260000];
int dist[260000];
vector < muchie > V;
int main()
{
int n,m;
bool flag = true;
fin >> n >> m;
V.resize(m);
for (auto &it : V)
fin >> it.x >> it.y >> it.c;
for (int i = 1 ; i <= n; i++)
{
tata[i] = 0;
dist[i] = inf;
}
dist[1] = 0;
for (int i = 1 ; i <= n; i++)
for (auto &it : V)
{
if (dist[it.x] + it.c < dist[it.y])
dist[it.y] = dist[it.x] + it.c;
tata[it.y] = it.x;
}
for (auto &it : V)
if (dist[it.x] + it.c < dist[it.y] && flag)
{
fout << "Ciclu negativ!";
flag = false;
}
if (flag)
for (int i = 2 ; i <= n; i++)
fout << dist[i] << " ";
return 0;
}