Pagini recente » Cod sursa (job #2482462) | Cod sursa (job #1830186) | Cod sursa (job #1347642) | Cod sursa (job #2372394) | Cod sursa (job #1486863)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 50001
#define INF 0x3f3f3f3f
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
struct grf
{
int nod,cost;
}aux;
class comp
{
public:
bool operator()(grf A, grf B)
{
return A.cost > B.cost;
}
};
priority_queue<grf, vector<grf>, comp> coada;
vector<grf> graf[MAXN];
int n,m,i,j,x,nod,cost;
int dist[MAXN];
int main()
{
cin>>n>>m;
for(i=1; i<=m; i++)
{
cin>>x>>aux.nod>>aux.cost;
graf[x].push_back(aux);
}
for(i=1; i<=n; i++)
dist[i]=INF;
dist[1]=0;
aux.nod=1; aux.cost=0;
coada.push(aux);
while(!coada.empty())
{
nod=coada.top().nod;
cost=coada.top().cost;
coada.pop();
if(dist[nod] != cost)
continue;
for(i=0; i<graf[nod].size(); i++)
if(dist[graf[nod][i].nod] > dist[nod] + graf[nod][i].cost)
{
dist[graf[nod][i].nod] = dist[nod] + graf[nod][i].cost;
aux.nod=graf[nod][i].nod; aux.cost = dist[graf[nod][i].nod];
coada.push(aux);
}
}
for(i=2; i<=n; i++)
cout<<dist[i]<<" ";
}