Pagini recente » Cod sursa (job #1077294) | Cod sursa (job #2729770) | Cod sursa (job #53164) | Cod sursa (job #73325) | Cod sursa (job #2672862)
#include <fstream>
#include <queue>
#include <vector>
#define INF 1000000000
using namespace std;
ifstream fi("dijkstra.in");
ofstream fo("dijkstra.out");
int N,M;
priority_queue < pair <int,int> > H;
int D[50001];
vector < pair <int, int> > ADJ[50001];
int main()
{
fi>>N>>M;
for (int i=1;i<=M;i++)
{
int a,b,c;
fi>>a>>b>>c;
ADJ[a].push_back({b,c});
ADJ[b].push_back({a,c});
}
D[1]=0;
for (int i=2;i<=N;i++)
D[i]=INF;
H.push({-D[1],1});
for (int i=1;i<=N;i++)
{
pair <int,int> p;
p=H.top();
H.pop();
int v,cost;
v=p.second;
cost=-p.first;
for (auto it:ADJ[v])
{
int w;
w=it.first;
int z;
z=it.second;
if (z+cost<D[w])
{
D[w]=z+cost;
H.push({-D[w],w});
}
}
}
for (int i=2;i<=N;i++)
fo<<D[i]<<" ";
fi.close();
fo.close();
return 0;
}