Pagini recente » Cod sursa (job #2279944) | Cod sursa (job #2481008) | Cod sursa (job #597645) | Cod sursa (job #1477948) | Cod sursa (job #650087)
Cod sursa(job #650087)
#include<fstream>
#include<iostream>
#include<vector>
using namespace std;
#define nmax 50001
int a[nmax],b[nmax],c[nmax];
int d[50001];
int n,m;
int main(){
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
f>>n>>m;
int i,j;
for(i=1;i<=m;i++){
f>>a[i]>>b[i]>>c[i];
if(i<=m)
d[i]=100000;
}
d[1]=0;
int ct=0;
short ok=0;
while(ok==0){
ok=1;
ct++;
for(i=1;i<=m;i++){
if(d[b[i]]>d[a[i]]+c[i]){
d[b[i]]=d[a[i]]+c[i];
ok=0;
}
}
if(ct>n){
g<<"Ciclu negativ!";
return 0;
}
}
for(i=2;i<=n;i++){
g<<d[i]<<" ";
}
return 0;
}