Pagini recente » Cod sursa (job #2774075) | Cod sursa (job #579202) | Cod sursa (job #2438610) | Cod sursa (job #181302) | Cod sursa (job #2691805)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
#include <cassert>
using namespace std;
const char iname[] = "bellmanford.in";
const char oname[] = "bellmanford.out";
const int MAX_N = 50005;
const int INF = 0x3f3f3f3f;
vector < pair <int, int> > adj[MAX_N];
vector <int> adj_t[MAX_N];
int main(void) {
ifstream in(iname);
int nodes, edges;
assert(in >> nodes >> edges);
assert(1 <= nodes && nodes <= 50000);
assert(1 <= edges && edges <= 250000);
for (int i = 0; i < edges; ++ i) {
int x, y, c;
assert(in >> x >> y >> c);
assert(1 <= x && x <= nodes);
assert(1 <= y && y <= nodes);
assert(-1000 <= c && c <= 1000);
adj[x].push_back(make_pair(y, c));
assert(find(adj_t[x].begin(), adj_t[x].end(), y) == adj_t[x].end());
adj_t[x].push_back(y);
}
in.close();
queue <int> Q;
bitset <MAX_N> in_queue(false);
vector <int> dist(MAX_N, INF), cnt_in_queue(MAX_N, 0);
int negative_cycle = false;
dist[1] = 0, Q.push(1), in_queue[1].flip();
while (!Q.empty() && !negative_cycle) {
int x = Q.front();
Q.pop();
in_queue[x] = false;
vector < pair <int, int> >::iterator it;
for (it = adj[x].begin(); it != adj[x].end(); ++ it) if (dist[x] < INF)
if (dist[it->first] > dist[x] + it->second) {
dist[it->first] = dist[x] + it->second;
if (!in_queue[it->first]) {
if (cnt_in_queue[it->first] > nodes)
negative_cycle = true;
else {
Q.push(it->first);
in_queue[it->first] = true;
cnt_in_queue[it->first] ++;
}
}
}
}
ofstream out(oname);
if (!negative_cycle) {
for (int i = 2; i <= nodes; ++ i)
out << dist[i] << " ";
}
else
out << "Ciclu negativ!\n";
out.close();
return 0;
}
/*#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <bitset>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n, m, ok;
const int nmax = 50005, val_mare = 0x3f3f3f3f;
int negative_cycle;
vector<pair<int, int> > list_vec[nmax];
int dist[nmax];
void bellman_ford_q(int start)
{
queue <int> q;
bitset <nmax> in_coada(false);
int contor_in_coada[nmax], i;
//vector <int> dist(MAX_N, INF), in_coada(MAX_N, 0);
for(i = 1; i <= n; ++i)
dist[i] = val_mare;
dist[start] = 0;
q.push(start);
in_coada[start] = 1;
int curent, vecin, cost;
while (!q.empty() && !negative_cycle)
{
curent = q.front();
q.pop();
in_coada[curent] = false;
for(auto it : list_vec[curent])
{
if(dist[curent] < val_mare)
{
vecin = it.first;
cost = it.second;
if (dist[vecin] > dist[curent] + cost)
{
dist[vecin] = dist[curent] + cost;
if (!in_coada[vecin])
{
if (contor_in_coada[vecin] > n)
negative_cycle = true;
else
{
q.push(vecin);
in_coada[vecin] = true;
contor_in_coada[vecin]++;
}
}
}
}
}
}
}
int main()
{
int u, v, c, i;
fin>>n>>m;
for(i = 1; i <= m; ++i)
{
fin>>u>>v>>c;
list_vec[u].push_back({v, c});
}
bellman_ford_q(1);
if(negative_cycle)
fout<<"Ciclu negativ!";
else
{
for(i = 2; i <= n; ++i)
fout<<dist[i]<<' ';
}
return 0;
}*/