Cod sursa(job #372723)

Utilizator filipbFilip Cristian Buruiana filipb Data 11 decembrie 2009 14:17:12
Problema Pitici Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <stdio.h>
//#include <vector>
using namespace std;
#define NMAX 1024
#define INF 2000000000
int A[NMAX][NMAX], B[NMAX][NMAX], C[NMAX][NMAX];
int lin[NMAX], col[NMAX], H[NMAX], D[NMAX], viz[NMAX];
int n, m, k, L;
void swap(int &x,int &y){
	int z=x; x=y; y=z;
}
void down_heap(int x){
	int son = 1;
	while(son){
		son = 0;
		if(2*x <= L) son = 2*x;
		if(2*x+1 <= L && C[H[2*x+1]][ col[H[2*x+1]] ] + D[H[2*x+1]] < C[H[2*x]][ col[H[2*x]] ] + D[H[2*x]] ) son = 2*x+1;
		if(son && C[H[son]][col[H[son]]] + D[H[son]] < C[H[x]][col[H[x]]] + D[H[x]])
			swap(H[son], H[x]);
		else son = 0;
	}
}
void up_heap(int x){
	while(x > 1 && C[H[x/2]][col[H[x/2]]] + D[H[x/2]] > C[H[x]][col[H[x]]] + D[H[x]]){
		swap(H[x], H[x/2]);
		x /= 2;		
	}
}
void push_heap(int x){
	H[++L] = x;
	up_heap(L);
}
int pop_heap(){
	int x = H[1];
	swap(H[1], H[L]);
	--L;
	down_heap(1);
	return x;
}
void dfs(int x){
	if(A[x][0]==0 || viz[x]) return;
	viz[x] = 1;
	
	int i;
	for(i = 1; i <= A[x][0]; ++i)
		dfs(A[x][i]);
	L = 0;	
	for(i = 1; i <= A[x][0]; ++i){
		col[A[x][i]] = 1;
		D[A[x][i]] = B[x][i];
		push_heap(A[x][i]);
		//if (x == 1)
		//	printf("---> %d %d\n", A[x][i], C[A[x][i]][col[A[x][i]]] + B[x][i]);
	}
	for(i = 1; i <= k; ++i){
		if (L <= 0)
			break;
		int y = pop_heap();
		if(C[y][col[y]] != INF) C[x][i] = C[y][col[y]] + D[y];
		else break;
	//	if (x == 1)
		//	printf("---> %d %d\n", y, C[y][col[y]] + D[y]);
		col[y]++;
		push_heap(y);
	}
}
int main(){
	freopen("pitici.in","r",stdin);
	freopen("pitici.out","w",stdout);
	int i, j;
	scanf("%d %d %d", &n, &m, &k);
	for(i = 1; i <= m; ++i){
		int a, b, c;
		scanf("%d %d %d", &a, &b, &c);
		A[a][++A[a][0]] = b;
		B[a][++B[a][0]] = c;
	}
	for(i = 1; i <= n; ++i)
		for(j = 1; j <= k; ++j)
			C[i][j] = INF;
	C[n][1] = 0;
	dfs(1);
	//for (i = 1; i <= n; ++i)
	//	printf("%d: %d %d %d %d %d\n", i, C[i][1],C[i][2],C[i][3],C[i][4]);
	for(j = 1; j < k; ++j)
		printf("%d ", C[1][j]);
	printf("%d\n", C[1][k]);
	return 0;
}