Pagini recente » Cod sursa (job #2561307) | Cod sursa (job #1360975) | Cod sursa (job #2099131) | Cod sursa (job #2241685) | Cod sursa (job #1326779)
#include<bits/stdc++.h>
using namespace std;
struct cell
{
int nod,cost;
bool operator <(const cell &e) const
{
return cost>e.cost;
}
};
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int oo=1<<30;
const int NMAX=50005;
int n,m,c[NMAX];
vector<cell>v[NMAX];
vector<cell>::iterator it;
priority_queue<cell>Q;
inline void DIJKSTRA()
{
int i;
cell k,aux;
for (i=2;i<=n;i++) c[i]=1<<30;
k.nod=1;k.cost=0;
Q.push(k);
while (!Q.empty())
{
k=Q.top();
Q.pop();
if (k.cost==c[k.nod])
{
for (it=v[k.nod].begin();it!=v[k.nod].end();it++)
if (c[(*it).nod]>(c[k.nod]+(*it).cost))
{
c[(*it).nod]=c[k.nod]+(*it).cost;
aux.nod=(*it).nod;
aux.cost=c[(*it).nod];
Q.push(aux);
}
}
}
}
int main()
{
int i,x;
cell k;
fin>>n>>m;
for (i=1;i<=m;i++)
{
fin>>x>>k.nod>>k.cost;
v[x].push_back(k);
swap(x,k.nod);
v[x].push_back(k);
}
DIJKSTRA();
for (i=2;i<=n;i++)
if (c[i]==oo) fout<<"0 ";
else fout<<c[i]<<" ";
fout<<"\n";
return 0;
}