Pagini recente » Cod sursa (job #768949) | Cod sursa (job #395360) | Cod sursa (job #239732) | Cod sursa (job #2198777) | Cod sursa (job #2787061)
#include <iostream>
#include <fstream>
#include <vector>
#include <tuple>
#define INF 1001
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n,m;
vector<tuple<int,int,int>>V;
int cost[250001];
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-1;j++) {
for(int i=0;i<m;i++) {
int a,b,w;
tie(a,b,w) = V[i];
cost[b] = min(cost[b],cost[a]+w);
}
}
for(int i=0;i<m;i++) {
int a,b,w;
tie(a,b,w) = V[i];
if(cost[b]>cost[a]+w) {
fout << "Ciclu negativ!";
return 0;
}
}
for(int i=2;i<=n;i++) fout << cost[i] << ' ';
return 0;
}