Pagini recente » Cod sursa (job #1333270) | Cod sursa (job #1555682) | Cod sursa (job #2313861) | Cod sursa (job #2276742) | Cod sursa (job #3197642)
#include <iostream>
#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<vector<node>> gf(50005);
int x, y, c, n, m, d[50005];
void bfs()
{
for(int i=2; i<=n; i++)
d[i]=inf;
d[1]=0;
q.push({1,0});
while(!q.empty())
{
int nod = q.top().ind;
for(auto nodcrt : gf[nod])
{
if(d[nodcrt.ind] > d[nod] + nodcrt.cost)
{
d[nodcrt.ind] = d[nod] + nodcrt.cost;
q.push({nodcrt.ind, d[nodcrt.ind]});
}
}
q.pop();
}
for(int i=2; i<=n; i++)
{
if(d[i]!=inf)
cout<<d[i]<<' ';
else
cout<<0<<' ';
}
}
int main()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
{
cin>>x>>y>>c;
gf[x].push_back({y,c});
}
bfs();
return 0;
}