Cod sursa(job #2133171)

Utilizator adriashkin.07alehandru69 adriashkin.07 Data 16 februarie 2018 17:06:40
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include<bits/stdc++.h>
using namespace std;
#define pb push_back

queue<int> C;
int n,m,D[100000],x,y,z;
struct nod
{
	int nr,lun;
	nod *next,*pr;
	
} *p,*r,*A[100000];
bool V[1000000];
	
int main()
{
//	ifstream cin("dijkstra.in");
//	ofstream cout("dijkstra.out");
	cin>>n>>m;
	for(int i=0;i<m;i++)
	  {
	  	r= new nod;
	  	cin>>x>>y>>z;
	  	r->nr=y;
	  	r->lun=z;
	  	r->next=A[x];
	  	if(A[x])A[x]->pr=r;
	    A[x]=r;
	  }
	C.push(1); V[1]=1;
	while(!C.empty())
	{ 
		r=A[C.front()];
		for(nod *i=r;i;i=i->next)
		{
		     if(!V[i->nr]) C.push(i->nr),D[i->nr]=i->lun+D[C.front()];
		     else {
		     	D[i->nr]=min(D[i->nr],D[C.front()]+i->lun);
		    if(!(i->next)) p=i;
			 }
		 }
		 V[C.front()]=1;
		 C.pop();
		     
	}
	for(int i=2;i<=n;i++) cout<<D[i]<<" ";

}