Pagini recente » Borderou de evaluare (job #1275215) | Borderou de evaluare (job #1423816) | Borderou de evaluare (job #364865) | Borderou de evaluare (job #1797237) | Cod sursa (job #1342501)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
struct muchie
{
int y, c;
};
const int N = 50001;
const int INF = (1 << 31) - 1;
int n, m;
int d[N];
bool ok[N];
vector<muchie> a[N];
queue<int> q;
void bellmanford(int x)
{
for(int i = 1; i <= n; i++)
d[i] = INF;
d[x] = 0;
q.push(x);
ok[x] = 1;
while(!q.empty())
{
x = q.front();
q.pop();
ok[x] = 0;
for(size_t i = 0; i < a[x].size(); i++)
{
int y = a[x][i].y;
int c = a[x][i].c;
if(d[x] + c < d[y])
{
d[y] = d[x] + c;
if(ok[y] == 0)
{
q.push(y);
ok[y] = 1;
}
}
}
}
}
void citire()
{
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
in >> x >> y >> c;
a[x].push_back((muchie){y, c});
}
}
void afisare()
{
for(int i = 2; i <= n; i++)
out << d[i] << ' ' ;
}
int main()
{
citire();
bellmanford(1);
afisare();
return 0;
}