Pagini recente » Cod sursa (job #2319470) | Cod sursa (job #1469637) | Cod sursa (job #235839) | Cod sursa (job #1165248) | Cod sursa (job #989191)
Cod sursa(job #989191)
#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 <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define maxn 50010
#define maxm 250010
#define inf 1000000000
#define pb push_back
#define mp make_pair
struct muchie
{
int a, b, c;
} e[maxm];
int n, m, i, j, k, left, right, cost[maxn], f[maxn];
vector <int> v[maxn];
vector <pair<int, int> > h;
void init()
{
int i;
cost[1]=0;
for(i=2; i<=n; i++)
cost[i]=inf;
h.pb(mp(0, 1));
make_heap(h.begin(), h.end());
}
void solve()
{
pair<int, int> per;
int i, j, nod, poz;
long long pas=0;
while(h.size() && pas<=1LL*n*m)
{
pas++;
pop_heap(h.begin(), h.end());
per=h.back();
h.pop_back();
nod=per.second;
f[nod]=0;
i = v[nod].size();
for(j=0; j<i; j++)
{
poz=v[nod][j];
if(cost[e[poz].a]+e[poz].c<cost[e[poz].b])
{
cost[e[poz].b]=cost[e[poz].a]+e[poz].c;
if(!f[e[poz].b])
{
f[e[poz].b]=1;
h.pb(mp(-cost[e[poz].b], e[poz].b));
push_heap(h.begin(), h.end());
}
}
}
}
}
long check_negativ()
{
int i;
for(i=1; i<=m; i++)
if(cost[e[i].a]+e[i].c<cost[e[i].b])
return 1;
return 0;
}
int main()
{
freopen("bellmanford.in", "rt", stdin);
freopen("bellmanford.out", "wt", stdout);
scanf("%d%d", &n, &m);
for(i=1; i<=m; i++)
{
scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c);
v[e[i].a].pb(i);
}
init();
solve();
if(check_negativ())
{
printf("Ciclu negativ!\n");
return 0;
}
for(i=2; i<=n; i++)
printf("%d ", cost[i]);
return 0;
}
*/