Pagini recente » Cod sursa (job #2616908) | Cod sursa (job #1673637) | Cod sursa (job #1924127) | Cod sursa (job #2688428) | Cod sursa (job #2739115)
#include <bits/stdc++.h>
using namespace std;
const int NMAX=1e5;
struct edge
{
int x, cost;
}e;
vector<edge>v[NMAX];
int dp[NMAX + 5];
bitset<NMAX+5>fr;
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int n , m , i , j , k , a, b,c;
scanf("%d%d",&n,&m);
for(i = 1 ; i <= m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
e.x=b;e.cost=c;
v[a].push_back(e);
}
for(i=2;i<=n;i++)dp[i]=NMAX*100;
dp[1] = 0;
queue<int>q;
q.push(1);
fr[1] = 1;
while(!q.empty())
{
int nod = q.front();
q.pop();
fr[nod] = 0;
for(auto it : v[nod])
{
if(dp[it.x] > dp[nod] + it.cost)
{
dp[it.x] = dp[nod] + it.cost;
if(!fr[it.x])
q.push(it.x);
fr[it.x]=1;
}
}
}
for(i = 2 ; i <= n ;i++)
if(dp[i]!=NMAX*100)printf("%d ",dp[i]);
else
printf("0 ");
return 0;
}