Pagini recente » Cod sursa (job #2890767) | Cod sursa (job #1566390) | Cod sursa (job #1075912) | Cod sursa (job #1628196) | Cod sursa (job #1834966)
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#define inf 200050
using namespace std;
int dist[50050];
class cmp
{
public :
const bool operator()(int &x,int &y)
{
return dist[x]>dist[y];
}
};
vector< vector< pair<int,int> > >g(50050);
priority_queue<int, vector<int>, cmp> pq;
bool viz[50050];
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int n,m,i,x,y,c,node,j=0;
scanf("%d%d",&n,&m);
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&c);
g[x].push_back(make_pair(y,c));
}
for(i=0; i<=n; i++)
dist[i]=inf;
dist[1]=0;
pq.push(1);
while(!pq.empty() && j!=n)
{
node=pq.top();
pq.pop();
if(viz[node]==false)
{
j++;
viz[node]=true;
for(i=0; i<g[node].size(); i++)
{
if(dist[node]+g[node][i].second<dist[g[node][i].first])
{
dist[g[node][i].first]=dist[node]+g[node][i].second;
pq.push(g[node][i].first);
}
}
}
}
for(i=2; i<=n; i++)
{
if(dist[i]!=inf)
printf("%d ",dist[i]);
else
printf("0 ");
}
return 0;
}