Pagini recente » Cod sursa (job #2310876) | Cod sursa (job #2675385) | Cod sursa (job #294153) | Cod sursa (job #1201912) | Cod sursa (job #1590632)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
const int nmax = 50005;
const int oo = (1<<29);
vector <pair<int,int> > g[nmax];
int dist[nmax], n, m;
class cmp{
public:
inline bool operator () (const int &x, const int &y)
{
return dist[x] > dist[y];
}
};
void dijkstra()
{
vector <pair<int,int> >::iterator it;
priority_queue <int, vector<int>, cmp> heap;
int dad, son, cost, i;
for(i=2; i<=n; i++)
dist[i]=oo;
heap.push(1);
while(!heap.empty())
{
dad=heap.top();
heap.pop();
for(it=g[dad].begin(); it!=g[dad].end(); it++)
{
son=it->first;
cost=it->second;
if(dist[dad]+cost < dist[son])
{
dist[son]=dist[dad]+cost;
heap.push(son);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
int 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));
}
dijkstra();
for(i=2; i<=n; i++)
{
if(dist[i]==oo) dist[i]=0;
fout << dist[i] << " ";
}
fin.close();
fout.close();
return 0;
}