#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define FICHERO "tasks.in"
#define MAX_BUFFER 4192
typedef struct tarea{
int d, tot_dep;
int *dep;
}TAREA;
TAREA *tasks;
int tot_tasks;
int dura(int);
void load_in_file();
int dura(int t)
{
int dur=tasks[t].d, i,j, max=0, d;
if(tasks[t].tot_dep > 1)
{
for(i=0; i<tasks[t].tot_dep; i++)
if((d=dura(tasks[t].dep[i])) > max)
max=d;
dur+=max;
}
else if(tasks[t].tot_dep)
dur+=dura(tasks[t].dep[0]);
return dur;
}
void load_in_file()
{
FILE *fp=fopen(FICHERO,"r");
if((fp=fopen(FICHERO,"r")))
{
char buffer[MAX_BUFFER], aux[MAX_BUFFER];
int tot,i,fin,j,h,k,l;
fgets(buffer, MAX_BUFFER, fp);
fgets(buffer, MAX_BUFFER, fp);
buffer[strlen(buffer)-1]=0;
tot_tasks=atoi(buffer);
fgets(buffer, MAX_BUFFER, fp);
fgets(buffer, MAX_BUFFER, fp);
tasks=malloc(tot_tasks*sizeof(TAREA));
for(i=0; i<tot_tasks; i++)
{
fgets(buffer, MAX_BUFFER, fp);
buffer[strlen(buffer)-1]=0;
j=0;
while(buffer[j])
{
h=0;
while(buffer[j] && buffer[j]!=',')
aux[h++]=buffer[j++];
aux[h]=0;
tasks[i].d=atoi(aux);
tasks[i].tot_dep=0;
j++;
}
}
fgets(buffer, MAX_BUFFER, fp);
fgets(buffer, MAX_BUFFER, fp);
while(fgets(buffer, MAX_BUFFER, fp))
{
buffer[strlen(buffer)-1]=0;
h=0;
j=0;
while(buffer[j] && buffer[j]!=',')
aux[h++]=buffer[j++];
aux[h]=0;
k=atoi(aux);
l=0;
j=0;
while(buffer[j])
{
if(buffer[j++]==',')
l++;
}
tasks[k].tot_dep=l;
if(tasks[k].tot_dep)
{
j=0;
while(buffer[j] && buffer[j++]!=',');
tasks[k].dep=malloc(tasks[k].tot_dep*sizeof(int));
l=0;
while(buffer[j])
{
h=0;
while(buffer[j] && buffer[j]!=',')
aux[h++]=buffer[j++];
aux[h]=0;
tasks[k].dep[l]=atoi(aux);
j++;
l++;
}
}
}
fclose(fp);
}
}
int main()
{
int i,j;
char c, aux[MAX_BUFFER];
load_in_file();
FILE *fp=fopen("/dev/stdin", "r");
while((c=fgetc(fp))!=EOF)
{
aux[0]=c;
i=1;
while((c=fgetc(fp))!=EOF && c!=',')
aux[i++]=c;
aux[i]=0;
printf("\n\n%d %d\n\n", atoi(aux), dura(atoi(aux)));
}
fclose(fp);
return 0;
}
$ulimit -s 32768
$time echo "999566" | ./tasks
999566 849
real 0m0.380s
user 0m0.363s
sys 0m0.013s
No da casi tiempo a verlo, pero chupará unos 25 MB de RAM (el tamaño máximo de la pila he tenido que subirlo a 32M, eso sí).
¡Ayyynns! El viejo C no me ha defraudado xD