Advertisement
Guest User

maree

a guest
Aug 9th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct nodo{
  5.     int num;
  6.     struct nodo* next;
  7. };
  8.  
  9. int solve(int N, int M, int T, int* S, int* E) {
  10.    
  11.     return solve2(N,M,T,S,E);
  12.    
  13. }
  14.  
  15. int solve2(int n, int m, int t, int* s, int* s2) {
  16.    
  17.     struct nodo** adj = (struct nodo**)malloc(n*sizeof(struct nodo));
  18.     int i;
  19.     for(i=0; i<n; i++)
  20.         adj[i] = NULL;
  21.    
  22.     struct nodo* e;
  23.    
  24.     for(i=0; i<m; i++){
  25.         if(adj[s[i]] == NULL){
  26.             adj[s[i]] = (struct nodo*)malloc(sizeof(struct nodo));
  27.             adj[s[i]]->num = s2[i];
  28.             adj[s[i]]->next = NULL;
  29.         }
  30.         else{
  31.             e = (struct nodo*)malloc(sizeof(struct nodo));
  32.             e->num = s2[i];
  33.             e->next = adj[s[i]];
  34.             adj[s[i]] = e;
  35.         }
  36.     }
  37.  
  38.     int tempi[n];
  39.    
  40.     tempi[0] = 0;
  41.     for(i=1; i<n; i++)
  42.         tempi[i] = -1;
  43.        
  44.     int cur;
  45.     struct nodo* inizio = (struct nodo*)malloc(sizeof(struct nodo));
  46.     inizio->num = 0;
  47.     inizio->next = NULL;
  48.     struct nodo* fine = inizio;
  49.    
  50.     while(inizio != NULL){
  51.        
  52.         cur = inizio->num;
  53.         inizio = inizio->next;
  54.        
  55.         e = adj[cur];
  56.        
  57.         while(e != NULL){
  58.            
  59.             if(tempi[e->num] > tempi[cur]+1 || tempi[e->num] == -1){
  60.                 tempi[e->num] = tempi[cur]+1;
  61.                
  62.                 if(inizio == NULL){
  63.                     inizio = (struct nodo*)malloc(sizeof(struct nodo));
  64.                     inizio->num = e->num;
  65.                     inizio->next = NULL;
  66.                     fine = inizio;
  67.                 }
  68.                 else{
  69.                     fine->next = (struct nodo*)malloc(sizeof(struct nodo));
  70.                     fine = fine->next;
  71.                     fine->num = e->num;
  72.                     fine->next = NULL;
  73.                 }
  74.             }
  75.            
  76.             e = e->next;
  77.         }
  78.     }
  79.  
  80.     int tempi2[n];
  81.    
  82.     tempi2[n-1] = 0;
  83.     for(i=0; i<n-1; i++)
  84.         tempi2[i] = -1;
  85.        
  86.     inizio = (struct nodo*)malloc(sizeof(struct nodo));
  87.     inizio->num = n-1;
  88.     inizio->next = NULL;
  89.     fine = inizio;
  90.    
  91.     while(inizio != NULL){
  92.        
  93.         cur = inizio->num;
  94.         inizio = inizio->next;
  95.        
  96.         e = adj[cur];
  97.        
  98.         while(e != NULL){
  99.            
  100.             if(tempi2[e->num] > tempi2[cur]+1 || tempi2[e->num] == -1){
  101.                 tempi2[e->num] = tempi2[cur]+1;
  102.                
  103.                 if(inizio == NULL){
  104.                     inizio = (struct nodo*)malloc(sizeof(struct nodo));
  105.                     inizio->num = e->num;
  106.                     inizio->next = NULL;
  107.                     fine = inizio;
  108.                 }
  109.                 else{
  110.                     fine->next = (struct nodo*)malloc(sizeof(struct nodo));
  111.                     fine = fine->next;
  112.                     fine->num = e->num;
  113.                     fine->next = NULL;
  114.                 }
  115.             }
  116.            
  117.             e = e->next;
  118.         }
  119.     }
  120.        
  121.     int min = -1;
  122.     for(i=0; i<n; i++)
  123.         if(tempi[i]  != -1)
  124.         if(tempi2[i] != -1)
  125.         if(tempi[i] <= t){
  126.             if(min == -1){
  127.                 min = t + tempi2[i];
  128.             }
  129.             else{
  130.                 if(min > t + tempi2[i])
  131.                     min = t + tempi2[i];
  132.             }
  133.         }
  134.        
  135.     return min;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement