Pagini recente » Cod sursa (job #1661388) | Cod sursa (job #1325198) | Cod sursa (job #1669249) | Cod sursa (job #1456853) | Cod sursa (job #1207428)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream ka("dijkstra.in");
ofstream ki("dijkstra.out");
const int N_MAX = 50000;
long long distanta[N_MAX + 1];
long long n, m, x, y, cost;
class nod
{
public:
long long index, cost;
bool operator < (const nod &arg) const
{
return cost > arg.cost;
}
};
vector< vector <nod> >graf(N_MAX+1);
priority_queue <nod> coada;
void dijkstra()
{
for(int i = 2; i <= n; i++)
{
distanta[i] = (1UL << 62);
}
nod n;
n.index = 1;
n.cost = 0;
coada.push(n);
while(!coada.empty())
{
nod n = coada.top();
coada.pop();
if(n.cost <= distanta[n.index])
{
distanta[n.index] = n.cost;
for(long long i = 0; i < graf[n.index].size(); i++)
{
if(n.cost + graf[n.index][i].cost < distanta[graf[n.index][i].index])
{
distanta[graf[n.index][i].index] = n.cost + graf[n.index][i].cost;
nod nn;
nn.index = graf[n.index][i].index;
nn.cost = distanta[graf[n.index][i].index];
coada.push(nn);
}
}
}
}
}
int main()
{
ka >> n >> m;
for(int i = 1; i <= m; i++)
{
ka >> x >> y >> cost;
nod no;
no.index = y;
no.cost = cost;
graf[x].push_back(no);
no.index = x;
graf[y].push_back(no);
}
dijkstra();
for(int i = 2; i <= n; i++)
{
if(distanta[i] == (1UL << 62))
ki << 0;
else
ki << distanta[i];
ki << " ";
}
}