Pagini recente » Cod sursa (job #1377448) | Cod sursa (job #148866) | Cod sursa (job #362216) | Cod sursa (job #735733) | Cod sursa (job #2364570)
#include <bits/stdc++.h>
using namespace std;
#define oo 100000000000000LL
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
struct muchie{long long y,cost;}aux;
vector<muchie>v[50005];
int n,m,x,y,c;
long long costv[50005];
bool viz[50005];
void Dijkstra(int x)
{
viz[x]=true;
for(int i=1;i<=n;++i)
costv[i]=oo;
costv[x]=0;
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] && (!k || costv[i]<costv[k]))k=i;
if(!k)break;
viz[k]=true;
for(auto h:v[k])
if(!viz[h.y] && costv[h.y]>costv[k]+h.cost)
costv[h.y]=costv[k]+h.cost;
}
}
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;
}