Pagini recente » Cod sursa (job #3227516) | Cod sursa (job #3293475) | Cod sursa (job #2552135) | Cod sursa (job #169054) | Cod sursa (job #372706)
Cod sursa(job #372706)
#include <stdio.h>
//#include <vector>
using namespace std;
#define NMAX 1024
#define INF 200000000
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(x==n || viz[x]) return;
viz[x] = 1;
int i;
for(i = 1; i <= A[x][0]; ++i)
dfs(A[x][i]);
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]);
}
for(i = 1; i <= k; ++i){
int y = pop_heap();
if(C[y][col[y]] != INF) C[x][i] = C[y][col[y]] + D[y];
else break;
col[y]++;
push_heap(y);
}
L = 0;
}
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(j = 1; j <= k; ++j)
printf("%d ", C[1][j]);
return 0;
}