Pagini recente » Cod sursa (job #1947135) | Cod sursa (job #66668) | Cod sursa (job #537249) | Cod sursa (job #372780) | Cod sursa (job #1592827)
// bellman-ford
#include <iostream>
#include <queue>
#include <fstream>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct nod
{
int val, cost;
};
vector <nod> grf[50001];
queue <int> coada;
int d[50001];
int n, m, i, a, b, c;
int main()
{
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>a>>b>>c;
nod act;
act.val=b;
act.cost=c;
grf[a].push_back(act);
}
for(i=1; i<=n; i++)
{
d[i]=100000;
}
coada.push(1); d[1]=0;
while(!coada.empty())
{
int cx=coada.front();
coada.pop();
for(i=0; i < grf[cx].size(); i++)
{
if(d[cx] + grf[cx][i].cost < d[grf[cx][i].val])
{
coada.push(grf[cx][i].val);
d[grf[cx][i].val] = d[cx]+grf[cx][i].cost;
}
}
}
for(i=2; i<=n; i++)
{
if(d[i]!=100000)
fout<<d[i]<<" ";
}
}