Pagini recente » Cod sursa (job #2640212) | Cod sursa (job #2986017) | Cod sursa (job #519349) | Cod sursa (job #269756) | Cod sursa (job #1866793)
#include <cstdio>
#define MAXN 1019
#define MAXM 200019
#define MAXK 1019
#define INF 1000000000
typedef struct{
int d, v;
}hp;
int cst[MAXN][MAXN];
int v[MAXN], dv;
int rez[MAXN][MAXK], dr[MAXN], pr[MAXN];
hp heap[MAXK];
int dh;
char viz[MAXN];
int n;
void srt(int x){
viz[x] = 1;
int i;
for(i = 0; i < n; i++){
if(!viz[i] && cst[x][i] != INF)
srt(i);
}
v[dv] = x;
dv--;
}
inline void intersch(int a, int b){
hp aux;
aux = heap[a];
heap[a] = heap[b];
heap[b] = aux;
}
inline void cobor(int x){
int best = x;
do{
x = best;
if(2 * x + 1 < dh && heap[best].d > heap[2 * x + 1].d)
best = 2 * x + 1;
if(2 * x + 2 < dh && heap[best].d > heap[2 * x + 2].d)
best = 2 * x + 2;
intersch(x, best);
}while(best != x);
}
inline void urc(int x){
while(x > 0 && heap[(x - 1) / 2].d > heap[x].d){
intersch(x, (x - 1) / 2);
x = (x - 1) / 2;
}
}
inline void add(hp x){
heap[dh] = x;
urc(dh);
dh++;
}
inline hp scot(){
hp rez = heap[0];
heap[0] = heap[dh - 1];
dh--;
cobor(0);
return rez;
}
int main(){
FILE *in = fopen("pitici.in", "r");
int m, k, x, y, z, poz, j, i;
fscanf(in, "%d%d%d", &n, &m, &k);
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
cst[i][j] = INF;
for(i = 0; i < m; i++){
fscanf(in, "%d%d%d", &x, &y, &z);
x--; y--;
cst[x][y] = z;
}
fclose(in);
dv = n - 1;
srt(0);
hp xh, s;
dr[0] = 1;
for(i = 1; i < n; i++){
dh = 0;
for(j = 0; j < n; j++){
if(cst[v[j]][v[i]] != INF){
pr[v[j]] = 1;
xh.d = rez[v[j]][0] + cst[v[j]][v[i]];
xh.v = v[j];
add(xh);
}
}
for(j = 0; j < k && dh != 0; j++){
s = scot();
rez[v[i]][j] = s.d;
if(pr[s.v] < dr[s.v]){
xh.d = rez[s.v][pr[s.v]++] + cst[s.v][v[i]];
xh.v = s.v;
add(xh);
}
}
dr[v[i]] = j;
}
FILE *out = fopen("pitici.out", "w");
for(i = 0; i < k; i++)
fprintf(out, "%d ", rez[n - 1][i]);
fclose(out);
return 0;
}