Pagini recente » Cod sursa (job #935232) | Cod sursa (job #2565166) | Cod sursa (job #1616165) | Cod sursa (job #1666137) | Cod sursa (job #1761437)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
#define NMAX 50001
const int INF=(1<<30);
queue <int> Q;
vector <pair <int,int> > H[NMAX];
int sol[NMAX];
int main()
{
int n,m;
int i,j;
in>>n>>m;
for(i=1;i<=m;i++)
{
int nod1,nod2,cost;
in>>nod1>>nod2>>cost;
H[nod1].push_back(make_pair(nod2,cost));
}
for(i=1;i<=n;i++)
{
sol[i]=INF;
}
sol[1]=0;
Q.push(1);
while(Q.empty()==0)
{
int nod=Q.front();
Q.pop();
for(i=0;i<H[nod].size();i++)
{
if(sol[H[nod][i].first] > sol[nod] + H[nod][i].second)
{
sol[H[nod][i].first]=sol[nod] + H[nod][i].second;
Q.push(H[nod][i].first);
}
}
}
for(i=2;i<=n;i++)
{
if(sol[i]==INF)
out<<0<<" ";
else
out<<sol[i]<<" ";
}
}