Pagini recente » Cod sursa (job #1885774) | Cod sursa (job #970127) | Cod sursa (job #939261) | Cod sursa (job #2050044) | Cod sursa (job #2576482)
#include <bits/stdc++.h>
#define MAX 50100
#define INF 999999999
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct nod
{
int x,cost;
};
vector<nod>g[MAX];
inline bool operator<(nod a,nod b)
{
return a.cost>b.cost;
}
priority_queue<nod>pq;
bool uz[MAX];
int d[MAX];
int n,m;
void dijkstra(int node);
int main()
{
int i,x,y,z;
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>x>>y>>z;
g[x].push_back({y,z});
}
dijkstra(1);
for(i=2; i<=n; i++)
if(d[i]==INF)
fout<<0<<' ';
else
fout<<d[i]<<' ';
return 0;
}
void dijkstra(int node)
{
int vecin,costt,nodcrt,i;
for(i=0; i<MAX; i++)
d[i]=INF;
pq.push({node,0});
d[node]=0;
uz[node]=1;
while(!pq.empty())
{
nodcrt=pq.top().x;
pq.pop();
uz[nodcrt]=0;
for(i=0; i<g[nodcrt].size(); i++)
{
vecin=g[nodcrt][i].x;
costt=g[nodcrt][i].cost;
if(d[vecin]>d[nodcrt]+costt)
{
d[vecin]=d[nodcrt]+costt;
if(!uz[vecin])
{
pq.push({vecin,d[vecin]});
uz[vecin]=1;
}
}
}
}
}