Pagini recente » Clasament 123455 | Cod sursa (job #1178256) | Clasament probleme_de_oni-runda2019 | Clasament rar12 | Cod sursa (job #3122612)
#include<iostream>
#include<fstream>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 1e9
using namespace std;
int i, n, m, x, y, cost, d[50005], ok = 1, frec[50005], inq[50005];
struct muchie
{
int x;
int y;
int cost;
};
vector<muchie> muc[50005];
queue <int> q;
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> x >> y >> cost;
muchie aux;
aux.x = x;
aux.y = y;
aux.cost = cost;
muc[x].push_back(aux);
}
fill(d + 2, d + n + 1, inf);
q.push(1);
while(!q.empty())
{
int nod = q.front();
frec[nod]++;
inq[nod]=0;
if(frec[nod]>n)
{
cout << "Ciclu negativ!";
return 0;
}
q.pop();
for(auto v: muc[nod])
{
if(d[v.y]>d[nod] + v.cost)
{
d[v.y] = d[nod] + v.cost;
if(!inq[v.y]){
q.push(v.y);
inq[v.y]=1;
}
}
}
}
for(int i = 2; i <= n; i++)
{
if(d[i] == inf)
cout << 0 << " ";
else
cout << d[i] << " ";
}
}