Pagini recente » Cod sursa (job #2093816) | Cod sursa (job #2915165) | Cod sursa (job #663710) | Cod sursa (job #1819058) | Cod sursa (job #3305475)
#include <fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
vector<pair<int,int>> se[50001];
bool einpq[50001];
int dist[50001];
struct cmp
{
bool operator()(int a,int b)
{
return dist[a]>dist[b];
}
};
priority_queue<int,vector<int>,cmp> pq;
void dijkstra(int sursa)
{
dist[sursa]=1;
pq.push(sursa);
while(!pq.empty())
{
int act=pq.top();
pq.pop();
einpq[act]=0;
for(auto vec:se[act])
{
int cost=dist[act]+vec.second;
if(dist[vec.first]==0 || cost<dist[vec.first])
{
dist[vec.first]=cost;
if(!einpq[vec.first])
{
pq.push(vec.first);
einpq[vec.first]=1;
}
}
}
}
}
int main()
{
int n,m,a,b,c;
in>>n>>m;
for(int i=1; i<=m; i++)
{
in>>a>>b>>c;
se[a].emplace_back(b,c);
}
dijkstra(1);
for(int i=2; i<=n; i++)
{
if(dist[i])
{
out<<dist[i]-1<<" ";
}
else
{
out<<0<<" ";
}
}
return 0;
}