Pagini recente » Cod sursa (job #669031) | Cod sursa (job #1903004) | Cod sursa (job #2806098) | Cod sursa (job #1819062) | Cod sursa (job #1980595)
#include <bits/stdc++.h>
#define nmax 50001
#define oo 10000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector<pair<int,int> >h[nmax];
priority_queue<pair<int,int> >mn;
int n,m,d[nmax],viz[nmax];
void Citire()
{
int x,y,c,i;
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>x>>y>>c;
h[x].push_back(make_pair(y,c));///graf orientat
}
}
void Dijkstra()
{
int i,x,p,q;
for(i=2; i<=n; i++)
d[i]=oo;
mn.push(make_pair(0,1));
while(!mn.empty())
{
x=mn.top().second;
mn.pop();
if(!viz[x])
{
viz[x]=1;
for(i=0; i<h[x].size(); i++)
{
q=h[x][i].first;
p=h[x][i].second;
if(d[q]>d[x]+p) ///am gasit un "cost" mai bun
{
d[q]=d[x]+p;
mn.push(make_pair(-d[q],q));/// ca valorea maxima sa fie "in fata"
}
}
}
}
for(i=2; i<=n; i++)
if(d[i]==oo)
fout<<"0 ";
else fout<<d[i]<<" ";
}
int main()
{
Citire();
Dijkstra();
fin.close();
fout.close();
return 0;
}