Pagini recente » Cod sursa (job #1673050) | Cod sursa (job #658276) | Cod sursa (job #2563149) | Cod sursa (job #1602522) | Cod sursa (job #2843507)
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
const int N=50001;
const int M=250001;
const int INF=1000000001;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
priority_queue<pair <int,int>>h;
vector<pair <int,int>>succesori[N];
int d[N],n,m;
bool selectat[N];
void dijkstra(int x0)
{
for (int i = 1; i <= n; i++)
{
d[i] = INF;
}
d[x0] = 0;
h.push({0,x0});
while (!h.empty())
{
int x = h.top().second;
h.pop();
if (selectat[x])
continue;
selectat[x]=1;
for (auto p:succesori[x])
{
int y=p.first;
int c=p.second;
if (d[x]+c<d[y])
{
d[y]=d[x]+c;
h.push({-d[y],y});
}
}
}
}
int main()
{
in>>n>>m;
for (int i=1;i<=m;i++)
{
int x, y, dist;
in>>x>>y>>dist;
succesori[x].push_back({y,dist});
}
dijkstra(1);
for (int i=2;i<=n;i++)
{
if (d[i]==INF)
d[i]=0;
out<<d[i]<<" ";
}
return 0;
}