Pagini recente » Cod sursa (job #1379239) | Cod sursa (job #1037656) | Borderou de evaluare (job #1588365) | Cod sursa (job #1675352) | Cod sursa (job #3322189)
#include <iostream>
#include <cstring>
#include <vector>
#include <set>
#include <fstream>
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
std::vector<pair<int,int>> muchii[50006];
int main() {
int n,m;
f>>n>>m;
for (int i=0;i<m;i++) {
int x,y,cost;
f>>x>>y>>cost;
muchii[x-1].emplace_back(y-1,cost);
}
int distance[50006];
for (int i=0;i<n;i++) {
distance[i]=100000000;
}
distance[0]=0;
for (int i=0;i<n;i++) {
bool ok=true;
for (int x=0;x<n;x++) {
for (auto [y,cost]:muchii[x]) {
if (distance[x]+cost<distance[y]) {
ok=false;
distance[y]=distance[x]+cost;
}
}
}
if (ok) {
break;
}
}
for (int i=0;i<n;i++) {
for (int x=0;x<n;x++) {
for (auto [y,cost]:muchii[x]) {
if (distance[x]+cost<distance[y]) {
g<<"Ciclu negativ!";
return 0;
}
}
}
}
for (int i=1;i<n;i++) {
g<<distance[i]<<" ";
}
}