Pagini recente » Cod sursa (job #2082985) | Cod sursa (job #2924783) | Cod sursa (job #727257) | Borderou de evaluare (job #1569015) | Cod sursa (job #2641960)
#include <iostream>
#include <fstream>
#include <vector>
#include <limits.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int INF = 1 << 30;
typedef pair<pair<int, int>, int> muchie;
int n, m;
vector<int> BellmanFordNeoptimizat(const vector<muchie> &muchii, int s)
{
vector<int> dist(n + 1, INF);
dist[s] = 0;
bool optim;
do
{
optim = true;
for(muchie m : muchii)
{
int x = m.first.first;
int y = m.first.second;
int cost = m.second;
if(dist[y] > dist[x] + cost)
{
dist[y] = dist[x] + cost;
optim = false;
}
}
} while(!optim);
return dist;
}
int main()
{
fin >> n >> m;
vector<muchie> muchii;
for(int i = 0; i < m; i++)
{
int x, y, cost;
fin >> x >> y >> cost;
muchii.push_back({{x, y}, cost});
}
vector<int> dist = BellmanFordNeoptimizat(muchii, 1);
for(int i = 2; i <= n; i++)
fout << dist[i] << ' ' ;
fout << '\n';
return 0;
}