Cod sursa(job #389681)

Utilizator ilincaSorescu Ilinca ilinca Data 2 februarie 2010 00:47:34
Problema Drumuri minime Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <stdio.h> 
#include <limits.h>
#include <math.h> 
#include <vector>
#include <queue>

#define nmax 1515
#define r 104659 
#define inf (double)INT_MAX

using namespace std;

typedef pair <double, int> di;

int n, m, np [nmax];
vector <di> g [nmax];
vector <double> d (nmax, inf); 
priority_queue <di, vector <di>, greater <di> > q;

void scan ()
{
	int i, a, b, c;
	double k;
	scanf ("%d%d", &n, &m);
	for (i=1; i <= m; ++i) 
	{
		scanf ("%d%d%d", &a, &b, &c);
		k=(double)log10 (c);
		g [a].push_back (di (k, b));
		g [b].push_back (di (k, a));
	}
}

void dijkstra ()
{
	vector <di> :: iterator it;
	int k;
	double da;
	q.push (di (0, 1));
	d [1]=0;
	np [1]=1;
	while (!q.empty ())
	{
		k=q.top ().second;
		da=q.top ().first;
		q.pop ();
		if (da > d [k]) continue;
		for (it=g [k].begin (); it != g [k].end (); ++it)
		{
			if (d [it->second] > d [k] + (it->first)) 
			{
				d [it->second] = d [k] + (it->first);
				q.push (di (d [it->second], it->second));
			}
		}	
	}	
}

inline double abs (double x)
{
	if (x < 0)
	       return -x;
	return x;	
}

void nrpos (int k)
{
	vector <di> :: iterator it;
	for (it=g [k].begin (); it != g [k].end (); ++it) 
		if (abs (d [k] - d [it->second] - (it->first)) < 0.00000001) 
		{
			if (np [it->second] == 0)
			       nrpos (it->second);	
			np [k] += np [it->second];
		}
}

void print ()
{
	for (int i=2; i <= n; ++i) 
//		printf ("%lf ", d [i]);
		printf ("%d ", np [i]);
//	printf ("\n%lf %lf %lf %lf %lf %lf %lf", log10(2), log10(3), log10(6), log10(24), log10(48), log10(72), log10 (144));
	printf ("\n");
}

int main ()
{
	freopen ("dmin.in", "r", stdin);
	freopen ("dmin.out", "w", stdout);
	scan ();
	dijkstra ();
	for (int i=2; i <= n; ++i) 
		if (np [i] == 0) 
			nrpos (i);
	print ();
	return 0;
}