Pagini recente » Cod sursa (job #1238148) | Cod sursa (job #1814129) | Cod sursa (job #1885332) | Cod sursa (job #1940925) | Cod sursa (job #2174770)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m;
priority_queue <pair<int,int> , vector <pair <int,int> > , greater <pair<int,int> > > pq;
vector <pair<int,int> > V[50005];
int dist[50005];
int main()
{
memset(dist,INF,sizeof(dist));
f>>n>>m;
for(int i=1;i<=m;++i)
{
int x,y,d;
f>>x>>y>>d;
V[x].push_back({y,d});
V[y].push_back({x,d});
}
dist[1]=0;
pq.push({0,1});
while(!pq.empty())
{
int nod = pq.top().second;
pq.pop();
for(auto &it:V[nod])
{
int u = it.first;
int di = it.second;
if(dist[u]>dist[nod]+di)
{
dist[u]=dist[nod]+di;
pq.push({dist[u],u});
}
}
}
for(int i=2;i<=n;++i)
g<<dist[i]<<" ";
return 0;
}