Pagini recente » Cod sursa (job #2559507) | Cod sursa (job #77670) | Cod sursa (job #265352) | Cod sursa (job #2558325) | Cod sursa (job #2553994)
#include <fstream>
#include <queue>
#include <vector>
#define f first
#define s second
#define pinf 1000000007
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
int n, m, c, x, y, d[50005], pmin, viz[50005], dmin;
vector < pair <int, int> > v[50005];
priority_queue < pair <int, int>, vector < pair <int, int> >, greater < pair <int, int> > > pq;
int main()
{
int i;
fin>>n>>m;
for (i=1; i<=m; i++)
{
fin>>x>>y>>c;
v[x].push_back(make_pair(c, y));
}
viz[1]=1;
for (auto it:v[1])
{
pq.push(it);
}
while (pq.empty()==0)
{
pmin=pq.top().s;
dmin=pq.top().f;
pq.pop();
if (viz[pmin]==0)
{
viz[pmin]=1;
d[pmin]=dmin;
for (auto it:v[pmin])
{
if (viz[it.s]==0)
{
pq.push(make_pair(d[pmin]+it.f, it.s));
}
}
}
}
for (i=2; i<=n; i++)
fout<<d[i]<<" ";
fin.close();
fout.close();
return 0;
}