Pagini recente » Cod sursa (job #1930006) | Cod sursa (job #294798) | Cod sursa (job #3185940) | Cod sursa (job #2362753) | Cod sursa (job #2364546)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
struct muchie{int y,cost;}aux;
vector<muchie>v[50005];
int n,m,x,y,c;
int costv[50005];
bool viz[50005];
void Dijkstra(int x)
{
viz[x]=true;
for(auto k:v[x]){
costv[k.y]=k.cost;
}
for(int j=1;j<=n-2;++j){
int k=0;
for(int i=1;i<=n;++i)
if(!viz[i] && costv[i] && (!k || costv[i]<costv[k]))k=i;
if(!k)break;
for(auto h:v[k])
if(!viz[h.y] && (!costv[h.y] || costv[h.y]>costv[k]+h.cost))
costv[h.y]=costv[k]+h.cost;
viz[k]=true;
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;++i){
f>>x>>y>>aux.cost;
aux.y=y;
v[x].push_back(aux);
aux.y=x;
v[y].push_back(aux);
}
Dijkstra(1);
for(int i=2;i<=n;++i)
g<<costv[i]<<' ';
f.close(); g.close();
return 0;
}