Pagini recente » Cod sursa (job #1014187) | Cod sursa (job #1380725) | Cod sursa (job #2204598) | Cod sursa (job #2485819) | Cod sursa (job #1918324)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int inf=1e9;
struct edge
{
int nod,c;
bool operator <(const edge &aux) const
{
return c>aux.c;
}
};
vector<edge> g[50010];
priority_queue<edge> h;
int d[50010],n;
void dijkstra(int nod)
{
for(int i=1;i<=n;i++) d[i]=inf;
d[nod]=0;
h.push({nod,0});
while(!h.empty())
{
int nod=h.top().nod,c=h.top().c;
h.pop();
if(c!=d[nod]) continue;
for(vector<edge>::iterator it=g[nod].begin();it!=g[nod].end();it++)
if(d[nod]+it->c<d[it->nod])
{
d[it->nod]=d[nod]+it->c;
h.push({it->nod,d[it->nod]});
}
}
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
int m,x,y,c;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&c);
g[x].push_back({y,c});
}
dijkstra(1);
for(int i=2;i<=n;i++)
if(d[i]==inf) printf("0 ");
else printf("%d ",d[i]);
return 0;
}