Pagini recente » Cod sursa (job #2688265) | Cod sursa (job #2733296) | Cod sursa (job #934503) | Cod sursa (job #3262879) | Cod sursa (job #2999837)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
#define nmax 50001
#define infinite 100000000
vector <pair <int, int>> a[nmax];
queue <int> coada;
int nodcurent,cost,vecin,i,j,n,m,k1,k2,k3;
int d[nmax];
bool ex[nmax];
void bellman()
{
while(coada.empty()==0)
{
nodcurent=coada.front();
coada.pop(); ex[nodcurent]=0;
for(unsigned int i=0; i<a[nodcurent].size();i++)
{
vecin=a[nodcurent][i].first;
cost=a[nodcurent][i].second;
if(d[vecin]>d[nodcurent]+cost)
{
d[vecin]=d[nodcurent]+cost;
if(ex[vecin]==0)
{
coada.push(vecin);
ex[vecin]=1;
}
}
}
}
}
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>k1>>k2>>k3;
a[k1].push_back(make_pair(k2,k3));
}
for(i=1;i<=n;i++)
d[i]=infinite;
coada.push(1); ex[1]=1; d[1]=0;
bellman();
for(i=2;i<=n;i++)
g<<d[i]<<" ";
return 0;
}