Pagini recente » Istoria paginii runda/concurs3ore/clasament | Cod sursa (job #881942) | Cod sursa (job #2057529) | Cod sursa (job #753832) | Cod sursa (job #2636824)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
const int inf = 1000000000;
int n, m;
vector<pair<pair<int, int>, int> > muchii;
int dist[50005];
void citire() {
fin >> n >> m;
while(m--) {
int x, y, c;
fin >> x >> y >> c;
muchii.push_back({{x, y}, c});
}
}
void solve() {
for(int i = 1; i<= n; i++)
dist[i] = inf;
dist[1] = 0;
for(int i = 1; i < n; i++)
for(auto x: muchii)
if(dist[x.first.first]+x.second < dist[x.first.second])
dist[x.first.second] = dist[x.first.first]+x.second;
for(auto x: muchii)
if(dist[x.first.first]+x.second < dist[x.first.second]) {
fout << "Ciclu negativ!";
return;
}
for(int i = 2; i<= n; i++)
fout << dist[i] << ' ';
}
int main() {
citire();
solve();
}