Pagini recente » Cod sursa (job #1911810) | Cod sursa (job #2301450) | Cod sursa (job #2619236) | Cod sursa (job #803143) | Cod sursa (job #3203503)
#include <bits/stdc++.h>
#define INF 2147483647
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
struct nod
{
int vecin, val;
bool operator < (const nod&other) const
{
if (val==other.val)
return vecin>other.vecin;
return val>other.val;
}
};
int n, m, x, y, c, d[50041]={INF};
bool viz[50041];
priority_queue<nod>q;
vector<nod>g[50041];
void read()
{
fin >> n >> m;
for (int i=1;i<=n;++i)
d[i]=INF;
for (int i=0;i<m;++i)
{
fin >> x >> y >> c;
nod t;t.val=c;t.vecin=y;
g[x].push_back(t);
}
}
void dijkstra(int top)
{
d[top]=0;
nod t;t.val=0;t.vecin=top;
q.push(t);
while(!q.empty())
{
nod cur=q.top();
viz[cur.vecin]=true;
q.pop();
for (auto i:g[cur.vecin])
{
//cout << d[i.vecin] << endl;
if (d[cur.vecin]+i.val<d[i.vecin]&&!viz[i.vecin])
{
d[i.vecin]=d[cur.vecin]+i.val;
t.val=d[i.vecin];
t.vecin=i.vecin;
q.push(t);
}
}
}
}
void afis()
{
for (int i=2;i<=n;++i)
{
if (d[i]!=INF)
fout << d[i] << " ";
else
fout << 0 << " ";
}
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(NULL);
read();
dijkstra(1);
afis();
return 0;
}