Advertisement
Guest User

CH4 Tasks

a guest
Jun 20th, 2011
1,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1.  
  2. /*
  3.  * Tuenti Contest
  4.  * Challenge 4 - Task Durations
  5.  * Author: Pedro Antonio Pardal Jimena
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #define IN_FILENAME "in"
  13. #define BUFFER_SIZE 0x1000
  14.  
  15. struct node
  16. {
  17.     unsigned int cost, child, brother;
  18. };
  19.  
  20. struct node_search
  21. {
  22.     unsigned int tid, cost;
  23. };
  24.  
  25. int total;
  26. struct node *table;
  27.  
  28. int stack_ptr = 0;
  29. struct node_search *stack;
  30.  
  31. #define stack_empty !stack_ptr
  32.  
  33. static inline void stack_push(struct node_search nod)
  34. {
  35.     stack[stack_ptr++] = nod;
  36. }
  37.  
  38. static inline struct node_search stack_pop()
  39. {
  40.     return stack[--stack_ptr];
  41. }
  42.  
  43. char buffer[BUFFER_SIZE];
  44.  
  45. static void read_table()
  46. {
  47.     int i, tid, cost, depends, brother;
  48.     char *tok;
  49.     FILE *in;
  50.    
  51.     in = fopen(IN_FILENAME, "r");
  52.    
  53.     if (in == NULL)
  54.     {
  55.         perror("fopen");
  56.         exit(-1);
  57.     }
  58.    
  59.     while (fgets(buffer, BUFFER_SIZE, in) != NULL)
  60.     {
  61.         if (buffer[0] != '#' && buffer[0] != '\0' && buffer[0] != '\n')
  62.         {
  63.             total = atoi(buffer);
  64.             table = (struct node *) malloc(total*sizeof(struct node));
  65.             stack = (struct node_search *) malloc(total*sizeof(struct node_search));
  66.  
  67.             break;
  68.         }
  69.     }
  70.    
  71.     for (i = 0; i < total; i++)
  72.     {
  73.         if (fgets(buffer, BUFFER_SIZE, in) == NULL)
  74.         {
  75.             perror("fgets");
  76.             exit(-1);
  77.         }
  78.    
  79.         if (buffer[0] == '#' || buffer[0] == '\0' || buffer[0] == '\n')
  80.         {
  81.             i--;
  82.             continue;
  83.         }
  84.        
  85.         tok = strtok(buffer, ",\n");
  86.         tid = atoi(tok);
  87.         tok = strtok(NULL, ",\n");
  88.         cost = atoi(tok);
  89.        
  90.         table[tid].cost = cost;
  91.         table[tid].child = -1;
  92.         table[tid].brother = -1;
  93.     }
  94.    
  95.     while (fgets(buffer, BUFFER_SIZE, in) != NULL)
  96.     {
  97.         if (buffer[0] != '#' && buffer[0] != '\0' && buffer[0] != '\n')
  98.         {
  99.             tok = strtok(buffer, ",\n");
  100.             tid = atoi(tok);
  101.            
  102.             brother = -1;
  103.            
  104.             while ((tok = strtok(NULL, ",\n")) != NULL)
  105.             {              
  106.                 depends = atoi(tok);
  107.                 table[depends].brother = brother;
  108.                 brother = depends;
  109.             }
  110.            
  111.             table[tid].child = brother;
  112.         }
  113.     }
  114.  
  115.     fclose(in);
  116. }
  117.  
  118. static void print_table()
  119. {
  120.     int i;
  121.  
  122.     for (i = 0; i < total; i++)
  123.     {
  124.         printf("Task %d: Cost->%d, Child->%d, Brother->%d\n",
  125.                 i, table[i].cost, table[i].child, table[i].brother);
  126.     }
  127. }
  128.  
  129. static int compute_cost(int tid)
  130. {
  131.     struct node_search nod = {.tid = tid, .cost = table[tid].cost};
  132.     int maxcost = -1, childcost = -1, child = -1;
  133.  
  134.     stack_push(nod);
  135.    
  136.     while (!stack_empty)
  137.     {
  138.         nod = stack_pop();
  139.  
  140.         childcost = nod.cost;
  141.        
  142.         if (childcost > maxcost)
  143.             maxcost = childcost;
  144.  
  145.         child = table[nod.tid].child;
  146.                
  147.         while (child != -1)
  148.         {
  149.             nod.tid = child;
  150.             nod.cost = table[child].cost + childcost;
  151.            
  152.             stack_push(nod);
  153.            
  154.             child = table[child].brother;
  155.         }
  156.     }
  157.    
  158.     return maxcost;
  159. }
  160.  
  161. int main(int argc, char *argv[])
  162. {
  163.     char *tok;
  164.     int tid, cost;
  165.  
  166.     read_table();
  167.        
  168.     if (fgets(buffer, BUFFER_SIZE, stdin) == NULL)
  169.     {
  170.         perror("fgets");
  171.         exit(-1);
  172.     }
  173.    
  174.     tok = strtok(buffer, ",\n");
  175.    
  176.     do
  177.     {
  178.         tid = atoi(tok);
  179.         cost = compute_cost(tid);
  180.        
  181.         fprintf(stdout, "%d %d\n", tid, cost);
  182.     }
  183.     while ((tok = strtok(NULL, ",\n")) != NULL);
  184.    
  185.     return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement