Pagini recente » Cod sursa (job #1272499) | Cod sursa (job #369371) | Cod sursa (job #2549135) | Cod sursa (job #1093474) | Cod sursa (job #2374563)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <iomanip>
#define ll long long
#define lsb(x) (x & -x)
using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
const int INF = 1e9;
int main() {
int n, m;
in >> n >> m;
vector<vector<pair<int, int>>> g(n + 1);
for(int i = 1; i <= m; i ++) {
int x, y, c;
in >> x >> y >> c;
g[x].push_back({y, c});
}
vector<int> dist(n + 1, INF);
dist[1] = 0;
for(int step = 1; step <= n; step ++) {
for(int i = 1; i <= n; i ++)
for(auto it : g[i])
dist[it.first] = min(dist[it.first], dist[i] + it.second);
}
for(int i = 1; i <= n; i ++)
for(auto it : g[i])
if(dist[it.first] - it.second > dist[i]) {
out << "Ciclu negativ!";
return 0;
}
for(int i = 2; i <= n; i ++)
out << dist[i] << " ";
return 0;
}