Pagini recente » Cod sursa (job #97458) | Cod sursa (job #302163) | Cod sursa (job #2159258) | Cod sursa (job #1004290) | Cod sursa (job #2576900)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 50005;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector<pair<int, int>> graf[NMAX];
int n, m;
long long dist[NMAX];
bool inCue[NMAX];
struct comp
{
bool operator()(int a, int b)
{
return dist[a]<dist[b];
}
};
void dij(int node)
{
priority_queue<int, vector<int>, comp> cue;
fill(dist,dist+NMAX,1<<31-1);
dist[node] = 0;
cue.push(node);
inCue[node]=1;
while(cue.size())
{
int top = cue.top();
cue.pop();
inCue[top]=false;
for(auto i:graf[top])
{
if(dist[i.first]>dist[top]+i.second)
{
dist[i.first] = dist[top]+i.second;
if(!inCue[i.first])
cue.push(i.first),inCue[i.first]=1;
}
}
}
}
int main()
{
fin>>n>>m;
while(m--)
{
int a, b, c;
fin>>a>>b>>c;
graf[a].push_back({b,c});
}
dij(1);
for(int i = 2;i<=n;i++)
{
if(dist[i]==1<<31-1)
fout<<0<<" ";
else
fout<<dist[i]<<" ";
}
return 0;
}