Pagini recente » Cod sursa (job #1326283) | Cod sursa (job #480865) | Cod sursa (job #1113848) | Cod sursa (job #1326295) | Cod sursa (job #1850434)
#include <bits/stdc++.h>
#define MAXN 50001
#define INF 0x3f3f3f3f
using namespace std;
ifstream in("djikstra.in");
ofstream out("djikstra.out");
int n,m,dist[MAXN];
struct edge
{
int nod,c;
edge(int nod, int c):nod(nod),c(c) {}
bool operator<(const edge &oth) const
{
return c>oth.c;
}
};
vector<edge> v[MAXN];
void djikstra(int start)
{
priority_queue<edge> pq;
for(int i=1; i<n; ++i)
dist[i]=INF;
dist[start]=0;
pq.push(edge(start,0));
while(!pq.empty())
{
int nod=pq.top().nod,c=pq.top().c;
pq.pop();
if(c>dist[nod]) continue;
for(edge& i : v[nod])
{
if(dist[nod]+i.c<dist[i.nod])
{
dist[i.nod]=dist[nod]+i.c;
pq.push(edge(i.nod,i.c));
}
}
}
}
int main()
{
in>>n>>m;
for(int i=0; i<m; ++i)
{
int a,b,c;
in>>a>>b>>c;
v[a-1].push_back(edge(b-1,c));
}
djikstra(0);
for(int i=1; i<n; ++i)
if(dist[i]==INF)out<<0<<' ';
else out<<dist[i]<<' ';
return 0;
}