Pagini recente » Cod sursa (job #337248) | Cod sursa (job #2933115) | Atasamentele paginii 795353277152115444 | Cod sursa (job #1435994) | Cod sursa (job #2574669)
#include<fstream>
#include<vector>
#include<set>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
set<pair<int,pair<int,int>>>G;
const int INF = 0x3f3f3f3f;
int D[50005];
int n,m;
void BF(int x)
{
}
int main()
{
fin>>n>>m;
for(int i=1;i<=n;i++)
D[i]=INF;
for(int i=1;i<=m;i++)
{
int x,y,w;
fin>>x>>y>>w;
G.insert({w,{x,y}});
}
D[1]=0;
BF(1);
for(int i=1;i<=n;i++)
{
for(auto p : G)
{
int w=p.first;
auto p2=p.second;
int x=p2.first;
int y=p2.second;
if(D[y]>D[x]+w)
D[y]=D[x]+w;
}
}
bool pp=false;
for(auto p : G)
{
int w=p.first;
auto p2=p.second;
int x=p2.first;
int y=p2.second;
if(D[y]>D[x]+w)
{
pp=true;
break;
}
}
if(pp)
{
fout<<"Ciclu negativ";
}
else
{
for(int i=2;i<=n;i++)
fout<<D[i]<<" ";
}
return 0;
}