Pagini recente » Cod sursa (job #598721) | Cod sursa (job #514407) | Cod sursa (job #646381) | Cod sursa (job #2931913) | Cod sursa (job #3279427)
#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 << 30);
int n, m;
int d[NMax];
bool use[NMax];
vector <pair <int, int> > g[NMax];
struct compara
{
bool operator()(int x, int y)
{
return d[x] > d[y];
}
};
priority_queue <int, vector<int>, compara> Q;
void read()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
g[x].push_back(make_pair(y, c));
}
}
void dijkstra(int nod)
{
for(int i = 1; i <= n; i++)
d[i] = oo;
d[nod] = 0;
Q.push(nod);
use[nod] = true;
while(!Q.empty())
{
int nod = Q.top();
Q.pop();
use[nod] = false;
for(size_t i = 0; i < g[nod].size(); i++)
{
int v = g[nod][i].first;
int c = g[nod][i].second;
if(d[nod] + c < d[v])
{
d[v] = d[nod] + c;
if(use[v] == false)
{
Q.push(v);
use[v] = true;
}
}
}
}
}
print()
{
for(int i = 2; i <= n; i++)
if(d[i] != oo)
fout << d[i] << " ";
else
fout << 0 << " ";
}
int main()
{
read();
dijkstra(1);
print();
return 0;
}