Pagini recente » Cod sursa (job #1027478) | Cod sursa (job #2150400) | Cod sursa (job #964404) | Cod sursa (job #1766904) | Cod sursa (job #1516912)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int nmax = 50005;
vector <pair<int,int> > g[nmax];
int dist[nmax];
class compare{
public:
bool operator () (const int &x, const int &y)
{
return dist[x] > dist[y];
}
};
int main()
{
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
int n, m, x, y, c, dad, cost, son, i;
priority_queue <int, vector<int>, compare> heap;
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]=(1<<29);
heap.push(1);
while(!heap.empty())
{
dad=heap.top();
heap.pop();
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;
heap.push(son);
}
}
}
for(i=2; i<=n; i++)
{
if(dist[i]==(1<<29)) fout << 0 << " ";
else fout << dist[i] << " ";
}
fin.close();
fout.close();
return 0;
}