Pagini recente » Cod sursa (job #917496) | Cod sursa (job #1491327) | Cod sursa (job #3256256) | Cod sursa (job #481670) | Cod sursa (job #3235585)
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int dim = 5e4 + 55;
bitset<dim>viz;
int ciclare[dim];
int dist[dim], n, m, x, y, ok, costu;
vector<pair<int, int> >noduri[dim];
void bellman(int nod)
{
queue<int>q;
q.push(nod);
while(!q.empty())
{
int nodu = q.front();
q.pop();
viz[nodu] = 0;
if(ciclare[nodu] >= n)
{
return;
}
for(auto it : noduri[nodu])
{
if(dist[nodu] + it.second < dist[it.first])
{
dist[it.first] = dist[nodu] + it.second;
if(viz[it.first] == 0)
{
q.push(it.first);
viz[it.first] = 1;
}
ciclare[it.first]++;
}
}
}
}
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int32_t main(int argc, char * argv[])
{
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
fin >> x >> y >> costu;
noduri[x].push_back({y, costu});
}
for(int i = 2; i <= n; ++i)
{
dist[i] = inf;
}
bellman(1);
for(int i = 2; i <= n; ++i)
{
fout << dist[i] << " ";
}
return 0;
}