Pagini recente » Cod sursa (job #2244067) | Cod sursa (job #650023) | Cod sursa (job #3245590) | Cod sursa (job #41852) | Cod sursa (job #398178)
Cod sursa(job #398178)
#include<algorithm>
using namespace std;
#include<vector>
#include<queue>
#define DIM 50005
int n,d[DIM];
vector <pair <int,int> > lst[DIM];
struct cmp
{
bool operator() (const int &a, const int &b) const
{
return d[a]>d[b];
}
}; priority_queue <int, vector <int>,cmp> c;
void read ()
{
int i,x,y,c,m;
scanf("%d%d",&n,&m);
for(i=2;i<=n;++i)
d[i]=1<<30;
for(i=1;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&c);
lst[x].push_back (make_pair (y,c));
}
}
void solve ()
{
int aux,i,j;
c.push (1);
while (!c.empty ())
{
aux=c.top ();
c.pop ();
for(j=0;j<lst[aux].size ();++j)
if(d[aux]+lst[aux][j].second<d[lst[aux][j].first])
{
d[lst[aux][j].first]=d[aux]+lst[aux][j].second;
c.push (lst[aux][j].first);
}
}
}
void show ()
{
int i;
for(i=2;i<=n;++i)
if(d[i]==1<<30)
printf("0 ");
else
printf("%d ",d[i]);
}
int main ()
{
freopen ("dijkstra.in","r",stdin);
freopen ("dijkstra.out","w",stdout);
read ();
solve ();
show ();
return 0;
}