Pagini recente » Cod sursa (job #1143953) | Cod sursa (job #1617512) | Cod sursa (job #2751774) | Cod sursa (job #741085) | Cod sursa (job #2772356)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n,m,x,y,c,d[50010];
bool v[50010];
vector<vector<pair<int,int>>>a;
priority_queue<pair<int,int>>q;
int main()
{
in>>n>>m;
a.resize(n+5);
for(int i=1;i<=m;++i)
{
in>>x>>y>>c;
a[x].push_back(make_pair(y,c));
}
q.push(make_pair(0,1));
for(int i=2;i<=n;++i)
{
d[i]=1000000010;
}
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(v[x]==true)
continue;
else
v[x]=true;
for(auto w:a[x])
{
if(w.second+d[x]<d[w.first])
{
d[w.first]=w.second+d[x];
q.push(make_pair(d[w.first],w.first));
}
}
}
for(int i=2;i<=n;++i)
{
if(d[i]==1000000010)
out<<0<<' ';
else
out<<d[i]<<' ';
}
return 0;
}