Pagini recente » Cod sursa (job #1967616) | Cod sursa (job #2855806) | Cod sursa (job #1432730) | Cod sursa (job #1621541) | Cod sursa (job #2364272)
#include <fstream>
#include <queue>
#include <string.h>
#include <vector>
#define nmax 50000
#define inf 0x3f3f3f3f
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct nod
{
int nd,c;
bool operator < (const nod &other)const
{
return c<other.c;
}
};
vector <nod> graf[nmax+5];
priority_queue<nod> que;
int n,m,dist[nmax+5];
int main()
{
fin>>n>>m;
int x,y,z,c,nod;
for(int i=1;i<=m;i++)
{
fin>>x>>y>>z;
graf[x].push_back({y,z});
}
memset(dist,inf,sizeof(dist));
dist[1]=0;
que.push({1,0});
while(!que.empty())
{
x=que.top().nd;
c=que.top().c;
que.pop();
if(dist[x]!=c)
continue;
for(auto y:graf[x])
if(dist[y.nd]>dist[x]+y.c)
{
dist[y.nd]=dist[x]+y.c;
que.push({y.nd,dist[y.nd]});
}
}
for(int i=2;i<=n;i++)
{
if(dist[i]==inf)
dist[i]=0;
fout<<dist[i]<<" ";
}
return 0;
}