Pagini recente » Cod sursa (job #2717084) | Ciorna | Cod sursa (job #129667) | Cod sursa (job #2458459) | Cod sursa (job #1331133)
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int n,m,i,x,y,c,d[50001];
bool o[50001];
vector<pair<int,int> > G[50001];
queue<int> Q;
void bellman_ford(int s)
{
int i,z,x;
for (i=1;i<=n;i++) d[i]=1000000000;
d[s]=0; Q.push(s);
while (!Q.empty())
{
x=Q.front();
o[x]=1;
Q.pop();
z=G[x].size();
for (i=0;i<z;i++)
if (d[G[x][i].first]>d[x]+G[x][i].second)
{
d[G[x][i].first]=d[x]+G[x][i].second;
if (!o[G[x][i].first]) Q.push(G[x][i].first);
}
}
}
int main()
{
freopen ("dijkstra.in","r",stdin);
freopen ("dijkstra.out","w",stdout);
scanf("%i%i",&n,&m);
for (i=1;i<=m;i++)
{
scanf("%i%i%i",&x,&y,&c);
G[x].push_back(make_pair(y,c));
}
bellman_ford(1);
for (i=2;i<=n;i++) printf("%i ",d[i]);
fclose(stdin);
fclose(stdout);
return 0;
}