Pagini recente » Cod sursa (job #1608893) | Cod sursa (job #2685971) | Cod sursa (job #1069485) | Cod sursa (job #2655683) | Cod sursa (job #2787070)
#include <iostream>
#include <fstream>
#include <vector>
#include <tuple>
#define INF 50000001
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n,m;
vector<tuple<int,int,int>>V;
int cost[50001];
int main() {
fin >> n >> m;
for(int i=1;i<=m;i++) {
int a,b,w;
fin >> a >> b >> w;
V.push_back({a,b,w});
}
for(int i=1;i<=n;i++) cost[i]=INF;
cost[1]=0;
for(int j=1;j<=n;j++) {
int ok=1;
for(int i=0;i<m;i++) {
int a,b,w;
tie(a,b,w) = V[i];
if(cost[b]>cost[a]+w) {
cost[b] = cost[a] + w;
ok=0;
}
}
if(ok==1) {
for(int i=2;i<=n;i++) fout << cost[i] << ' ';
return 0;
}
}
fout << "Ciclu negativ!";
return 0;
}