Pagini recente » Cod sursa (job #2191172) | Cod sursa (job #1867761) | Cod sursa (job #2456994) | Cod sursa (job #1204099) | Cod sursa (job #1275018)
#include <cstdio>
#include <vector>
#include <queue>
#define nmax 50005
#define pb push_back
#define inf 999999999
using namespace std;
vector <pair<int, int> > a[nmax];
queue <int> q;
int n, m, i, j, x, y, cost, dmin[nmax];
bool viz[nmax];
int main ()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d%d", &n, &m);
i=1;
while(m)
{
scanf("%d%d%d", &x, &y, &cost);;
a[x].pb(make_pair(y, cost));
dmin[i]=inf;
--m; ++i;
}
int nod;
vector<pair<int, int> >::iterator it;
q.push(1); dmin[1]=0; viz[1]=1;
while(!q.empty())
{
nod=q.front();
q.pop();
viz[nod]=0;
for(it=a[nod].begin(); it!=a[nod].end(); ++it)
{
if(dmin[nod]+it->second <dmin[it->first])
{
dmin[it->first]=it->second+dmin[nod];
if(!viz[it->first])
{
q.push(it->first);
viz[it->first]=1;
}
}
}
}
for(i=2; i<=n; ++i)
printf("%d ", (dmin[i]<inf?dmin[i]:0));
return 0;
}