Pagini recente » Cod sursa (job #2353485) | Cod sursa (job #1079176) | Cod sursa (job #3303464) | Cod sursa (job #2172118) | Cod sursa (job #3341435)
#include <fstream>
#include<bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define INF 1e9
typedef pair<int,int> pr;
const int NMAX=1e5;
int d[NMAX+5];
vector<pr>graph[NMAX+5];
set<pr>pq;
int main()
{
int n,m;
fin>>n>>m;
while(m--)
{
int x,y,c;
fin>>x>>y>>c;
graph[x].push_back({y,c});
graph[y].push_back({x,c});
}
for(int i=1; i<=n; i++)
{
d[i]=INF;
}
d[1]=0;
pq.insert({0,1});
while(!pq.empty())
{
auto [cost,nod]=*pq.begin();
pq.erase(pq.begin());
for(auto x:graph[nod])
{
auto [y,costdrum]=x;
if(d[y]>costdrum+cost)
{
d[y]=costdrum+cost;
pq.insert({d[y],y});
}
}
}
for(int i=2;i<=n;i++)
{
if(d[i]==INF){fout<<0<<" ";}
else
fout<<d[i]<<" ";
}
return 0;
}