Cod sursa(job #2683918)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 12 decembrie 2020 11:18:54
Problema Drumuri minime Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 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.000000000001;
}

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;

bool inq[1541];

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;
	
	pth[1] = 1;
	pq.push({1,0});
	while(!pq.empty()){
		auto nd = pq.top();pq.pop();
		int a = nd.a;
		inq[a] = false;
		
		for(auto ed : gra[a]){
			int b = ed.b;
			double d = ed.d;
			if(dst[a]+d < dst[b]){
				dst[b] = dst[a]+d;
				if(!inq[b])pq.push({b,dst[b]});
				inq[b] = true;
				
				pth[b] = pth[a];
			}else if(eq(dst[a]+d, dst[b])){
				pth[b] += pth[a];
				pth[b] %= MODIT;
			}
		}
	}
	
	for(int i = 2; i <= n; ++i){
		fout << pth[i] << " ";
	}
	
	return 0;
}