Pagini recente » Cod sursa (job #928111) | Cod sursa (job #941801) | Cod sursa (job #219707) | Cod sursa (job #3243783) | Cod sursa (job #839356)
Cod sursa(job #839356)
#include<fstream>
#include<queue>
#include<vector>
#define NMAX 50003
#define INF 100000000
using namespace std;
ifstream f("bellmanford.in"); ofstream g("bellmanford.out");
int n, m, viz[NMAX];
short nod_a, nod_b, cost;
queue<int> q;
struct nod
{
int v;
short c;
nod(){};
nod(int a,short b)
{
v =a;
c = b;
};
};
vector<nod> x[NMAX];
inline void citire();
inline void BF(int);
int main()
{
citire();
BF(1);
for(int i = 2; i <= n; ++i)
g<<viz[i]<<" ";
g.close();
return 0;
}
inline void citire()
{
f>>n>>m;
for(int i = 1; i <= m; ++i)
{
f>>nod_a>>nod_b>>cost;
x[nod_a].push_back(nod(nod_b, cost));
if(i <= n) viz[i] = INF;
}
}
inline void BF(int k)
{
q.push(k);
viz[k] = 0;
while(!q.empty())
{
k = q.front(); q.pop();
vector<nod>::iterator it;
for(it = x[k].begin(); it != x[k].end(); ++it)
if((*it).c + viz[k] < viz[(*it).v])
{
viz[(*it).v]= (*it).c + viz[k];
q.push((*it).v);
}
}
}