Pagini recente » Cod sursa (job #285862) | Cod sursa (job #814138) | Cod sursa (job #1141213) | Cod sursa (job #734937) | Cod sursa (job #2204279)
#include <fstream>
#include <queue>
#include <cstring>
#include <vector>
#define D 50001
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct str{
int n,c;
bool operator < (const str &other) const
{
return c>other.c;
}
} x;
priority_queue <str> pq;
vector <str> v[D];
int dist[D], vectr;
int n,m,a,b,c;
void dijkstra()
{
pq.push({1,0});
dist[1]=0;
while(!pq.empty())
{
x=pq.top();
pq.pop();
if(dist[x.n]<x.c)
continue;
for(auto i : v[x.n])
{
if(dist[i.n]>x.c+i.c)
{ dist[i.n]=x.c+i.c;
pq.push({i.n,dist[i.n]});
}
}
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>a>>b>>c;
v[a].push_back({b,c});
}
for(int i=1;i<=D;i++)
dist[i]=INF;
dijkstra();
for(int i=2;i<=n;i++)
{
if(dist[i]==INF)
fout<<0<<' ';
else fout<<dist[i]<<' ';
}
return 0;
}