Pagini recente » Cod sursa (job #1624039) | Cod sursa (job #2162196) | Cod sursa (job #2746520) | Cod sursa (job #1766249) | Cod sursa (job #2147135)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMax = 50005;
const int oo = (1 << 30);
int N, M;
int D[NMax];
bool marca[NMax];
vector < pair <int,int> > v[NMax];
struct compara
{
bool operator()(int x, int y)
{
return D[x] > D[y];
}
};
priority_queue<int, vector<int>, compara> q;
void Citeste()
{
fin >> N >> M;
for(int i = 1; i <= M; i++)
{
int x, y, c;
fin >> x >> y >> c;
v[x].push_back(make_pair(y,c));
}
}
void Dijkstra()
{ int x;
int nod ,cost;
for(int i = 2; i <= N; i++)
D[i] = oo;
D[1]=0;
q.push(1);
marca[1]=true;
while(!q.empty())
{
x=q.top();
q.pop();
for(int i=0;i<v[x].size();i++)
{
nod=v[x][i].first;
cost=v[x][i].second;
if(!marca[nod])
{
D[nod]=D[x]+cost;
q.push(nod);
marca[nod]=true;
}
}
}
}
void Afiseaza()
{
for(int i = 2; i <= N; i++)
{
if(D[i] != oo)
fout << D[i] << " ";
else
fout << "0 ";
}
}
int main()
{
Citeste();
Dijkstra();
Afiseaza();
}