Pagini recente » Profil Alex_Badescu | Cod sursa (job #1974448)
#include <bits/stdc++.h>
#define Nmax 50001
#define INF 2e9+5
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct graph
{
vector < pair<int,int> > v;
};
graph G[Nmax];
int cost[Nmax];
priority_queue < pair<int,int> > pq;
#define val first
#define poz second
int main()
{int n,m,i,j,k,x,y;
f>>n>>m;
for(k=1;k<=m;k++)
{
f>>i>>j>>x;
G[i].v.push_back({j,x});
}
fill(cost+2,cost+n+1,INF);
pq.push({0,1});
while(!pq.empty())
{
x=pq.top().poz;
y=pq.top().val;
pq.pop();
for(i=0;i<G[x].v.size();i++)
if(cost[G[x].v[i].first]>y+G[x].v[i].second)
{
cost[G[x].v[i].first]=y+G[x].v[i].second;
pq.push({cost[G[x].v[i].first],G[x].v[i].first});
}
}
for(i=2;i<=n;i++)
if(cost[i]==INF) g<<0<<' ';
else g<<cost[i]<<' ';
return 0;
}