Pagini recente » Cod sursa (job #976358) | Cod sursa (job #1883689) | Cod sursa (job #1429668) | Cod sursa (job #1457167) | Cod sursa (job #2553190)
#include <iostream>
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
#define pinf 1000000007
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
int n, m, viz[50010];
unsigned long long d[50010];
pair<int,int> elem, minn, aux;
priority_queue<pair<int,int>,vector<pair<int,int> >, std::greater<pair<int,int> > > q;
vector<pair<int,int> >v[50010];
void citire ()
{
int x, y, co, i, j;
fin>>n>>m;
for (i=1; i<=m; i++)
{
fin>>x>>y>>co;
elem.first=y;
elem.second=co;
v[x].push_back(elem);
}
for (i=2; i<=n; i++)
d[i]=pinf;
for(i=0; i<v[1].size(); i++)
{
elem.first=v[1][i].second;
elem.second=v[1][i].first;
d[v[1][i].first]=v[1][i].second;
q.push(elem);
}
}
void dijkstra ()
{
int i, j;
viz[1]=1;
while (!q.empty())
{
minn=q.top();
q.pop();
viz[minn.second]=1;
for (j=0; j<v[minn.second].size(); j++)
{
if (d[v[minn.second][j].first]>d[minn.second]+v[minn.second][j].second)
{
d[v[minn.second][j].first]=d[minn.second]+v[minn.second][j].second;
elem.first=d[v[minn.second][j].first];
elem.second=j;
q.push(elem);
}
}
}
}
void afisare ()
{
int i;
for (i=2; i<=n; i++)
if (d[i]==pinf)fout<<0<<' ';
else fout<<d[i]<<' ';
}
int main()
{
citire();
dijkstra();
afisare();
fin.close();
fout.close();
return 0;
}