Pagini recente » Cod sursa (job #1956581) | Cod sursa (job #368025) | Cod sursa (job #3037457) | Cod sursa (job #2328665) | Cod sursa (job #1843404)
#include <cstdio>
#include <vector>
#include <queue>
#define inf 2000050
using namespace std;
int dist[50050];
struct edge
{
int node;
const bool operator<(const edge &node2)const
{
return dist[node]>dist[node2.node];
}
};
vector< vector< pair<int,int> > >g(50050);
priority_queue<edge>pq;
bool viz[50050];
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int x1,y1,c,m,n,i,j;
edge x,y;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x1,&y1,&c);
g[x1].push_back(make_pair(y1,c));
}
for(i=0;i<=n;i++)
dist[i]=inf;
dist[1]=0;
x.node=1;
pq.push(x);
i=0;
while(!pq.empty() && i!=n)
{
x=pq.top();
pq.pop();
if(viz[x.node]==false)
{
i++;
viz[x.node]=true;
for(j=0;j<g[x.node].size();j++)
{
if(dist[x.node]+g[x.node][j].second<dist[g[x.node][j].first])
{
dist[g[x.node][j].first]=dist[x.node]+g[x.node][j].second;
y.node=g[x.node][j].first;
pq.push(y);
}
}
}
}
for(i=2;i<=n;i++)
{
if(dist[i]!=inf)
printf("%d ",dist[i]);
else
printf("0 ");
}
return 0;
}