Pagini recente » Cod sursa (job #1140552) | Cod sursa (job #1218951) | Cod sursa (job #2055803) | Cod sursa (job #2895403) | Cod sursa (job #2531576)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define pii pair <int,int>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct str{
int nod;
int cost;
bool operator <(const str&other) const
{
return cost>other.cost;
}
};
int n,m,drum[50005];
vector <pii> v[50005];
priority_queue <str> pq;
void djk()
{
while(!pq.empty())
{
int nod=pq.top().nod;
int cost=pq.top().cost;
pq.pop();
for(auto it=v[nod].begin();it<v[nod].end();it++)
{
int nxt=(*it).first;
int cst=(*it).second;
if(drum[nod]+cst<drum[nxt])
{
drum[nxt]=drum[nod]+cst;
pq.push({nxt,drum[nxt]});
}
}
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,z;
fin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
memset(drum,INF,sizeof(drum));
drum[1]=0;
pq.push({1,0});
djk();
for(int i=2;i<=n;i++)
{
if(drum[i]==INF)
fout<<0<<" ";
else
fout<<drum[i]<<" ";
}
return 0;
}