Pagini recente » Cod sursa (job #1861801) | Cod sursa (job #2554802) | Cod sursa (job #2403911) | Cod sursa (job #708160) | Cod sursa (job #2563823)
#include <bits/stdc++.h>
#define cost first
#define v second
using namespace std;
typedef pair < double, int > pdi;
const int NMAX = 1505;
const int INF = 2000000000;
const int MOD = 104659;
ifstream fin("dmin.in");
ofstream fout("dmin.out");
int N, M, x, y, c;
vector < int > Ad[NMAX];
vector < double > Cost[NMAX];
double dist[NMAX];
int D[NMAX];
bool viz[NMAX];
priority_queue < pdi, vector < pdi >, greater < pdi > > H;
void Read()
{
fin >> N >> M;
for( int i = 1; i <= M; ++i )
{
fin >> x >> y >> c;
Ad[x].push_back( y );
Cost[x].push_back( log(c) );
Ad[y].push_back( x );
Cost[y].push_back( log(c) );
}
}
bool EGAL( double A, double B )
{
return abs(A-B) <= 0.000000001;
}
void Dijkstra( int v )
{
for( int i = 1; i <= N; ++i )
dist[i] = INF;
dist[v] = 1;
D[v] = 1;
H.push( make_pair( 1, v ) );
while( H.size() )
{
int nod = H.top().v;
H.pop();
//cout << nod << ' ' << dist[nod] << '\n';
if( viz[nod] ) continue;
else viz[nod] = 1;
for( int i = 0; i < Ad[nod].size(); ++i )
{
int w = Ad[nod][i];
double dw = Cost[nod][i];
//cout << nod << ' ' << w << ' ' <<dist[w] << ' ' << dist[nod] + dw << '\n';
if( EGAL(dist[w], dist[nod] + dw ) )
D[w] += D[nod];
else
if( dist[w] > dist[nod] + dw )
{
dist[w] = dist[nod] + dw;
D[w] = D[nod];
H.push( make_pair( dist[w], w ) );
}
}
}
for( int i = 1; i <= N; ++i )
if( i != v )
{
fout << D[i] << ' ';
}
}
void Solve()
{
Dijkstra( 1 );
}
int main()
{
Read();
Solve();
return 0;
}