Pagini recente » Cod sursa (job #547928) | Cod sursa (job #1448912) | Cod sursa (job #2129141) | Cod sursa (job #6671) | Cod sursa (job #1548495)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
const int nmax = 5e4+5;
const int inf = (1<<29);
vector <pair<int,int> > g[nmax];
int dist[nmax];
inline bool cmp(const int &x, const int &y)
{
return dist[x] > dist[y];
}
void dijkstra()
{
vector <int> h;
int dad, son, i, cost;
h.push_back(1);
make_heap(h.begin(), h.end(), cmp);
while(!h.empty())
{
dad=h.front();
pop_heap(h.begin(), h.end(), cmp);
h.pop_back();
for(i=0; i<g[dad].size(); i++)
{
son=g[dad][i].first;
cost=g[dad][i].second;
if(dist[son] > dist[dad]+cost)
{
dist[son]=dist[dad]+cost;
h.push_back(son);
push_heap(h.begin(), h.end(), cmp);
}
}
}
}
int main()
{
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
ios_base::sync_with_stdio(false);
int n, m, i, x, y, c;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> x >> y >> c;
g[x].push_back(make_pair(y, c));
}
for(i=2; i<=n; i++)
dist[i]=inf;
dijkstra();
for(i=2; i<=n; i++)
{
if(dist[i]==inf) dist[i]=0;
fout << dist[i] << " ";
}
fin.close();
fout.close();
return 0;
}