Pagini recente » Cod sursa (job #2052835) | Cod sursa (job #1783712) | Cod sursa (job #1639990) | Cod sursa (job #1263821) | Cod sursa (job #2361147)
#include <bits/stdc++.h>
#define N 101
#define INF 100001
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector<pair<int,int>> v[N];
queue<int> q;
int n,m,nod,d[N];
bool viz[N];
//pereche de tipul v[nod] = {nod,cost}
int main()
{
int i,a,b,c;
fin>>n>>m;
while(fin>>a>>b>>c)
{
v[a].push_back(make_pair(b,c));
//v[b].push_back(make_pair(a,c));
}
for(i=1;i<=n;i++)
d[i]=INF;
d[1]=0;
q.push(1);
while(!q.empty())
{
int vf = q.front();
q.pop();
if(!viz[vf])
{
viz[vf]=true;
for(auto s:v[vf])
{
if(d[vf]+s.second<d[s.first])
{
d[s.first]=d[vf]+s.second;
q.push(s.first);
viz[s.first]=false;
}
}
}
}
for(i=2;i<=n;i++)
if(d[i]==INF)
fout<<"0 ";
else
fout<<d[i]<<" ";
return 0;
}