Pagini recente » Cod sursa (job #1930914) | Cod sursa (job #2451143) | Cod sursa (job #1532824) | Cod sursa (job #960058) | Cod sursa (job #1834879)
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#define inf 20050
using namespace std;
struct edge
{
int node,cost;
const bool operator<(const edge &node2)const
{
return cost>node2.cost;
}
};
vector< vector< pair<int,int> > >g(50050);
priority_queue<edge>pq;
int dist[50050];
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));
}
memset(dist,inf,sizeof(dist));
dist[1]=0;
x.cost=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;
y.cost=dist[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;
}