Pagini recente » Cod sursa (job #705333) | Cod sursa (job #314184) | Cod sursa (job #341003) | Cod sursa (job #440808) | Cod sursa (job #3244101)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
struct HeapNode
{
int nod;
long long dist_tot;
bool operator <(const HeapNode &other) const
{
return dist_tot>other.dist_tot;
}
};
priority_queue <HeapNode> pq;
struct drum
{
int cost, nod;
};
vector <drum> matad[50005];
long long dist[50005];
void dijkstra(int n)
{
HeapNode prim;
prim.nod=1;
prim.dist_tot=0;
pq.push(prim);
while(!pq.empty())
{
HeapNode top=pq.top();
pq.pop();
if(dist[top.nod]<top.dist_tot)
{
continue;
}
int p=matad[top.nod].size();
for(int i=0; i<p; i++)
{
int destinatie=matad[top.nod][i].nod;
int costdestinatie=matad[top.nod][i].cost;
if(top.dist_tot+costdestinatie<dist[destinatie])
{
dist[destinatie]=top.dist_tot+costdestinatie;
HeapNode vecin;
vecin.nod=destinatie;
vecin.dist_tot=dist[destinatie];
pq.push(vecin);
}
}
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++)
dist[i]=0x3f3f3f3f;
for(int i=1; i<=m; i++)
{
int a,b,cost;
cin>>a>>b>>cost;
drum x;
x.nod=b;
x.cost=cost;
matad[a].push_back(x);
}
dijkstra(n);
for(int i=2; i<=n; i++)
{
if(dist[i]==0x3f3f3f3f)
dist[i]=0;
cout<<dist[i]<<" ";
}
return 0;
}