Cod sursa(job #977312)

Utilizator tudorv96Tudor Varan tudorv96 Data 25 iulie 2013 16:12:50
Problema Drumuri minime Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;

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

#define N 1505
#define xx first
#define yy second
#define MOD 104659
#define NEXT 1e9

typedef long double def;
typedef pair <def, int> nod;
typedef pair <nod, int> all;

const def oo = 1e5;
const def er = 1e-8;

vector <nod> g[N];
int f[N], n, m;
bool q[N];
def d[N];
priority_queue <all, vector <all>, greater <all> > H;

int main() {
	fin >> n >> m;
	while (m--) {
		int x, y; def z;
		fin >> x >> y >> z;
		def Z = (z != 1) ? log(z) : 1;
		g[x].push_back (nod (Z, y));
		g[y].push_back (nod (Z, x));
	}
	fin.close();
	for (int i = 1; i <= n; ++i)
		d[i] = oo;
	H.push(all (nod (0, 1), 1));
	while (H.size()) {
		def crt = H.top().xx.xx; int x = H.top().xx.yy, fr = H.top().yy; H.pop();
		if (d[x] < oo - er && (d[x] < crt - er || d[x] > crt + er))
			continue;
		if (d[x] == oo) {
			d[x] = crt;
			H.push(all (nod(crt, x), NEXT));
		}
		if (fr != NEXT) {
			f[x] = (f[x] + fr) % MOD; 
			continue;
		}
		for (vector <nod> :: iterator it = g[x].begin(); it != g[x].end(); ++it)
			if (d[(*it).yy] == oo)
				H.push(all (nod (d[x] + (*it).xx, (*it).yy), f[x]));
	}		
	for (int i = 2; i <= n; ++i)
		fout << f[i] << " ";
	fout.close();
}