Cod sursa(job #402847)

Utilizator loginLogin Iustin Anca login Data 24 februarie 2010 10:49:47
Problema Drumuri minime Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
# include <fstream>
# include <cmath>
# define INFINIT 1000000000
using namespace std;
struct nod {
	int capat;
	double cost;
	nod *next;};
int n, nrd[15003], v[15003], t[15003], h[15003], poz[15003], nh;
double d[15003];
nod *g[15003];

void add (int i, int j, int c)
{
	nod *p=new nod;
	p->capat=j;
	p->cost=log10((double)c);
	p->next=g[i];
	g[i]=p;
}

void read ()
{
	ifstream fin ("dmin.in");
	int m;
	fin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		h[i]=i;
		poz[i]=i;
		d[i]=INFINIT;
	}
	nh=n;
	int i, j, c;
	for (;m;--m)
	{
		fin>>i>>j>>c;
		add(i, j, c);
		add(j, i, c);
	}
}

void cerne (int k, int n)
{
	int i, eh=0;
	while (!eh && 2*k<=n)
	{
		i=2*k;
		if (i+1<=n && d[h[i+1]]<d[h[i]])
			i++;
		if (d[h[i]]>=d[h[k]])
			eh=1;
		else
		{
			int aux;
			aux=h[i];h[i]=h[k];h[k]=aux;
			poz[h[i]]=i;
			poz[h[k]]=k;
			k=i;
		}
	}
}

void promoveaza (int k)
{
	int eh=0;
	while (!eh && k/2)
		if (d[h[k]]>=d[h[k/2]])
			eh=1;
		else
		{
			int aux;
			aux=h[k];h[k]=h[k/2];h[k/2]=aux;
			poz[h[k]]=k;
			poz[h[k/2]]=k/2;
			k/=2;
		}
}		

void dijkstra ()
{
	int q;
	d[1]=0;
	v[1]=1;
	for (nod *p=g[1];p;p=p->next)
	{
		d[p->capat]=p->cost;
		t[p->capat]=1;
		promoveaza(poz[p->capat]);
	}
	h[1]=h[nh];
	poz[h[1]]=1;
	nh--;
	nrd[1]=1;
	cerne (1, nh);
	for (int l=1;l<n;l++)
	{
		q=h[1];
		h[1]=h[nh];
		poz[h[1]]=1;
		cerne(1, nh);
		v[q]=1;
		for (nod *p=g[q];p;p=p->next)
		{
			int j=p->capat;
			if (!v[j] && d[j]>d[q]+p->cost)
			{
				t[j]=q;
				d[j]=d[q]+p->cost;
				promoveaza(poz[j]);
				nrd[j]=1;
			}
			else
				if(!v[j] && d[j]==d[q]+p->capat)
				{
					nrd[j]+=nrd[q];
					if (nrd[j]==104659);
						nrd[j]=0;
				}
		}
	}
}
	
void afis ()
{
	ofstream fout ("dmin.out");
	for (int i=2;i<=n;i++)
		fout<<nrd[i]<<" ";
}

int main ()
{
	read ();
	dijkstra ();
	afis ();
	return 0;
}