Pagini recente » Cod sursa (job #1794520) | Cod sursa (job #178230) | Cod sursa (job #2351385) | Cod sursa (job #2059935) | Cod sursa (job #3255469)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
int const lmax=50007;
int const vmax=20007;
struct h{
int node;
int cost;
int dcur;
bool operator<(const h& a) const
{
if(dcur!=a.dcur)return dcur>a.dcur;
else if(cost==a.cost)
{
return node>a.node;
}
return cost > a.cost;
}
/*bool operator==(const h& a )const
{
return cost==a.cost;
}*/
};
vector <h> G[lmax];
int d[lmax];
bool viz[lmax];
priority_queue <h> pq;
void dijskstra(int root)
{
d[root]=0;
viz[root]=1;
/*for(auto vec:G[root])
{
pq.push(vec);
d[vec.node]=vec.cost;
vec.dcur=vec.cost;
}*/
for(int i=0;i<G[root].size();i++)
{
d[G[root][i].node]=G[root][i].cost;
G[root][i].dcur=G[root][i].cost;
pq.push(G[root][i]);
}
while(pq.empty()==false)
{
h best=pq.top();
pq.pop();
viz[best.node]=1;
/*for(auto vec:G[best.node])
{
if(viz[vec.node]==0 and d[vec.node]>d[best.node]+vec.cost)
{
pq.push(vec);
d[vec.node]=d[best.node]+vec.cost;
vec.dcur=d[vec.node];
}
}*/
for(int i=0;i<G[best.node].size();i++)
{
if(viz[G[best.node][i].node]==0 and d[G[best.node][i].node]>d[best.node]+G[best.node][i].cost)
{
d[G[best.node][i].node]=d[best.node]+G[best.node][i].cost;
G[best.node][i].dcur=d[G[best.node][i].node];
pq.push(G[best.node][i]);
}
}
}
}
int n, m;
int main()
{
fin>>n>>m;
for(int i=1;i<=n;i++)
{
d[i]=vmax*lmax;
}
int a,b,c;
while(fin>>a>>b>>c)
{
G[a].push_back({b,c,lmax*vmax});
}
dijskstra(1);
for(int i=2;i<=n;i++)
{
if(d[i]==vmax*lmax)
{
fout<<0<<" ";
continue;
}
fout<<d[i]<<" ";
}
fin.close();
fout.close();
return 0;
}