Cod sursa(job #784429)
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
#define Nmax 1511
#define Mmax 2511
#define EPS 1e-10
#define val first
#define key second
#define oo 1<<30
vector< pair<double,int> > A[Nmax];
vector< pair<double,int> > Aux;
queue< int > Q;
double Cost[Nmax];
int Cont[Nmax];
int N,M;
bool Used[Nmax];
ifstream F("dmin.in");
ofstream G("dmin.out");
int main()
{
F>>N>>M;
for (int i=1;i<=M;++i)
{
int a,b;
double c;
F>>a>>b>>c;
c=log(c);
A[a].push_back( make_pair( c , b ) );
A[b].push_back( make_pair( c , a ) );
}
for (int i=1;i<=N;++i)
Cost[i]=oo,
Cont[i]=0;
int Start=1;
Cost[Start]=0;
Cont[Start]=1;
Q.push( Start );
Used[Start]=1;
while ( Q.size() )
{
int Nbr=Q.front();
Q.pop();
Used[Nbr]=0;
while ( A[Nbr].size() )
{
Aux.push_back( A[Nbr].back() );
A[Nbr].pop_back();
if ( Cost[Aux.back().key] + EPS > Cost[Nbr] + Aux.back().val )
{
Cost[Aux.back().key] = Cost[Nbr] + Aux.back().val ;
if ( !Used[ Aux.back().key ] )
{
Q.push( Aux.back().key );
Used[ Aux.back().key ]=1;
}
}
if ( fabs( Cost[Aux.back().key] - ( Cost[Nbr] + Aux.back().val ) ) < EPS )
Cont[Aux.back().key] += Cont[Nbr] ;
}
while ( Aux.size() )
{
A[Nbr].push_back( Aux.back() );
Aux.pop_back();
}
}
for (int i=2;i<=N;++i)
G<<Cont[i]<<' ';
G<<'\n';
}