Pagini recente » Monitorul de evaluare | Cod sursa (job #502048) | Monitorul de evaluare | Cod sursa (job #125521) | Cod sursa (job #3197659)
#include <fstream>
#include <vector>
#include <queue>
#define inf 300001
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
struct node
{
int ind;
int cost;
bool operator < (const node &other) const
{
return (cost>other.cost);
}
};
priority_queue<node> q;
vector<node> gf[50005];
int x, y, c, n, m, d[50005];
bool viz[50005];
void bfs()
{
d[1]=0;
q.push({1,0});
while(!q.empty())
{
int nod = q.top().ind;
q.pop();
if(viz[nod])
continue;
viz[nod]=true;
for(auto nodcrt : gf[nod])
{
int vecin = nodcrt.ind;
int cost = nodcrt.cost;
if(d[vecin] > d[nod] + cost)
{
d[vecin] = d[nod] + cost;
q.push({vecin,d[vecin]});
}
}
}
for(int i=2; i<=n; i++)
{
if(d[i]!=inf)
cout<<d[i]<<' ';
else
cout<<0<<' ';
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(int i=1; i<=m; i++)
{
if(i<=n)
d[i]=inf;
cin>>x>>y>>c;
gf[x].push_back({y,c});
}
bfs();
return 0;
}