Pagini recente » Cod sursa (job #1745281) | Cod sursa (job #2136337) | Cod sursa (job #947059) | Cod sursa (job #2346772) | Cod sursa (job #1897907)
#include <fstream>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector <pair<int,int>> a[500005];
priority_queue <pair<int,int>> h;
int n,m;
int x,y,c;
int d[50005];
bool viz[50005],prel[50005];
int main()
{
fin>>n>>m;
for (int i=0;i<m;i++) {
fin>>x>>y>>c;
a[x].push_back(make_pair(y,c));
}
d[1]=0;
//memset(d,250000,n);
for (int i = 1; i <= n; i++) {
d[i] = 1000000000;
}
d[1] = 0;
h.push(make_pair(0,1));
while (!h.empty()) {
do {
x=h.top().second;
h.pop();
}while (!h.empty() && prel[x]);
//fout<<"sel vf:"<<x<<'\n';
prel[x]=true;
//d[x]=-d[x];
for (int i=0;i<a[x].size();i++) {
y=a[x][i].first;
if (d[x]+a[x][i].second<d[y])
d[y]=d[x]+a[x][i].second;
h.push(make_pair(-d[y],y));
}
}
for (int i=2;i<=n;i++) fout<<d[i]<<' ';
}