Pagini recente » Cod sursa (job #873662) | Cod sursa (job #327862) | Cod sursa (job #1316333) | Cod sursa (job #344152) | Cod sursa (job #2713608)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector <pair<int,int> > a[250010];
int n,m,d[50010];
bool v[50010];
struct nod
{
int x;
// constructor
nod(int new_x)
{
x = new_x;
}
};
inline bool operator<(const nod& a, const nod& b)
{
return d[a.x]<d[b.x];
}
priority_queue<nod,vector<nod>> Q;
int main()
{
f>>n>>m;
for(int i=2; i<=n; i++)
d[i]=999999;
for(int i=1; i<=m; i++)
{ int x,y,c;
f>>x>>y>>c;
a[x].push_back(make_pair(c,y));
}
Q.push(nod(1));
while(!Q.empty())
{ int node=Q.top().x;
v[node]=1;
Q.pop();
for(auto pr:a[node])
{ int vec = pr.second;
int cost_m = pr.first;
if(d[vec]>d[node]+cost_m)
{ d[vec]=d[node]+cost_m;
Q.push(nod(vec));
}
}
}
for(int i=2;i<=n;i++)
if(v[i])g<<d[i]<<' ';
else g<<'0'<<' ';
return 0;
}