Cod sursa(job #2683898)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 12 decembrie 2020 10:59:03
Problema Drumuri minime Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

ifstream fin("dmin.in");
ofstream fout("dmin.out");

const double INF = 100000;
const int MODIT = 104659;

bool eq(double lhs, double rhs){
	return abs(lhs-rhs) <= 0.000001;
}

struct edg{
	int b;double d;
};
struct nod{
	int a;double d;
	bool operator<(const nod &rhs)const{
		if(d != rhs.d){
			return d > rhs.d;
		}else{
			return a > rhs.a;
		}
	}
};

int n, m;
vector<edg> gra[1541];

double dst[1541];
int pth[1541];
priority_queue<nod> pq;

vector<int> pak[1541];

void dfs(int a){
	for(auto b : pak[a]){
		if(pth[b] == 0)dfs(b);
		pth[a] += pth[b];
	}
}

int main(){
	// ios_base::sync_with_stdio(false);
	
	fin >> n >> m;
	for(int i = 0; i < m; ++i){
		int a, b, fd;
		fin >> a >> b >> fd;
		
		double d = log(fd);
		gra[a].push_back({b,d});
		gra[b].push_back({a,d});
	}
	
	for(int i = 2; i <= n; ++i)dst[i] = INF;
	
	pq.push({1,0});
	while(!pq.empty()){
		auto nd = pq.top();pq.pop();
		int a = nd.a;
		if(nd.d > dst[a])continue;
		
		for(auto ed : gra[a]){
			int b = ed.b;
			double d = ed.d;
			if(dst[a]+d < dst[b]){
				dst[b] = dst[a]+d;
				pq.push({b,dst[b]});
				
				pak[b].clear();
				pak[b].push_back(a);
			}else if(eq(dst[a]+d, dst[b])){
				pak[b].push_back(a);
			}
		}
	}
	
	pth[1] = 1;
	for(int i = 2; i <= n; ++i){
		if(pth[i] == 0)dfs(i);
	}
	
	for(int i = 2; i <= n; ++i){
		fout << pth[i] << " ";
	}
	
	return 0;
}