Cod sursa(job #389755)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 2 februarie 2010 11:00:45
Problema Drumuri minime Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include<cstdio>
#define N 1501
#include<math.h>
#include<vector>
#define pb push_back
#define MOD 104659
#define INF 1<<30;
using namespace std;
short int n,m,x,y,coada[N*N];
int z,nr[N],rez[N];
double d[N];
bool viz[N];
const double eps=1e-2;
vector<short int>a[N],ap(N,0);
vector<double>c[N];
void citire()
{
	freopen("dmin.in","r",stdin);
	freopen("dmin.out","w",stdout);
	scanf("%hd%hd",&n,&m);
	for (int i=1; i<=m; ++i)
	{
		scanf("%hd%hd%d",&x,&y,&z);
		double g=log(z);
		a[x].pb(y);
		c[x].pb(g);
		a[y].pb(x);
		c[y].pb(g);
	}
}
void bf()
{
	int p=0,u=0;
	coada[u++]=1;
	nr[1]=1;
	viz[1]=true;
	while (u!=p)
	{
		x=coada[p++];
		viz[x]=false;
		size_t g=a[x].size();
		for (size_t i=0; i!=g; ++i)
		{
			y=a[x][i];
			double aux=d[x]+c[x][i],diff=d[y]-aux;
			if (diff>eps)
			{
				d[y]=aux;
				nr[y]=nr[x];
				if (nr[y]>=MOD)
					nr[y]-=MOD;
				if (!viz[y])
				{
					++ap[y];
					if (ap[y]>n)
						return;
					viz[y]=true;
					coada[u++]=y;
				}
			}
			else
				//if (diff==0)
				{
					nr[y]+=nr[x];
					if (nr[y]>=MOD)
						nr[y]-=MOD;
				}
		}
		rez[x]=nr[x];
		nr[x]=0;
	}
}
void afis()
{
	for (int i=2; i<=n; ++i)
		printf("%d ",rez[i]);
}
int main()
{
	citire();
	for (int j=2; j<=n; ++j)
		d[j]=INF;
	bf();
	afis();
	return 0;
}