Advertisement
DimasDark

L4Q3

Aug 18th, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdio>
  4.  
  5. #define max(a,b) (a > b ? a : b)
  6. using namespace std;
  7.  
  8.  
  9. struct no
  10. {
  11.     int info;
  12.     struct no *proximo;
  13. };
  14.  
  15. class pilha
  16. {
  17.     struct no *topo;
  18. public:
  19.     pilha();
  20.     void push(int);
  21.     int pop();
  22.     bool isVazia();
  23.     void mostrar();
  24. };
  25.  
  26. pilha::pilha()
  27. {
  28.     this->topo = NULL;
  29. }
  30.  
  31. void pilha::push(int v)
  32. {
  33.     no *p = new no;
  34.     p->info = v;
  35.     p->proximo = NULL;
  36.     if(topo!=NULL)
  37.     {
  38.         p->proximo = topo;
  39.     }
  40.     topo = p;
  41. }
  42.  
  43. int pilha::pop()
  44. {
  45.     struct no *temp;
  46.     int valor;
  47.     if(topo!=NULL)
  48.     {
  49.         temp = topo;
  50.         topo = topo->proximo;
  51.         valor = temp->info;
  52.         delete temp;
  53.     }
  54.     return valor;
  55. }
  56.  
  57. bool pilha::isVazia()
  58. {
  59.     return (topo == NULL);
  60. }
  61.  
  62. void pilha::mostrar()
  63. {
  64.     struct no *p = topo;
  65.     if(topo!=NULL)
  66.     {
  67.         while(p!=NULL)
  68.         {
  69.             if (p->proximo != NULL)
  70.             cout<<p->info<< " ";
  71.             else
  72.                 cout<<p->info<< ".";
  73.             p = p->proximo;
  74.         }
  75.     }
  76. }
  77.  
  78.  
  79. int matrix[10000][10000] = {0};
  80. int escolhas[10000][10000] = {0};
  81.  
  82. //nItems = numero de items
  83. //capMax = capacidade maxima da "mochila"
  84. //os outros 2 arrays porque fica ruim de inserir bidimensional
  85.  
  86.  
  87. int knapsack(int nItems, int capMax, int tempo[], int saudades[]){
  88.     int i,j;
  89.  
  90.     for (i=1;i<=nItems;i++){
  91.         for (j=0;j<=capMax;j++){
  92.             if (tempo[i-1]<=j){
  93.                 matrix[i][j] = max(matrix[i-1][j],saudades[i-1]+matrix[i-1][j-tempo[i-1]]);
  94.                 if (saudades[i-1]+matrix[i-1][j-tempo[i-1]]>matrix[i-1][j])
  95.                     escolhas[i][j]=1;
  96.                 else
  97.                     escolhas[i][j]=-1;
  98.             }
  99.             else{
  100.                 escolhas[i][j] = -1;
  101.                 matrix[i][j] = matrix[i-1][j];
  102.             }
  103.         }
  104.     }
  105.  
  106.     return matrix[nItems][capMax];
  107.  
  108. }
  109.  
  110. void imprimirEscolhas(int N, int capMax, int tempo[]){
  111.       pilha p;
  112.       int maximo = N-1;
  113.       while (N > 0){
  114.         if (escolhas[N][capMax]!=-1){
  115. //            printf("Val: %d", N-1);
  116.             p.push(N-1);
  117.             N--;
  118.             capMax -= tempo[N];
  119.         }
  120.         else{
  121.             N--;
  122.         }
  123.     }
  124.     p.mostrar();
  125.  
  126.     printf("\n");
  127.  
  128. return;
  129. }
  130.  
  131. int main()
  132. {
  133.     freopen("L4Q3.in", "r", stdin);
  134.     freopen("L4Q3.out", "w", stdout);
  135.  
  136.     int N; //amigos
  137.     while (cin >> N)
  138.     {
  139.     if (N != 0)
  140.     {
  141.         string amigo;
  142.         int* saudades = new int[N+1];
  143.         int* tempo = new int[N+1];
  144.         int T; //tempo
  145.         cin >> T;
  146.  
  147.         for (int i = 0; i < N;i++){
  148.             cin >> amigo;
  149.             cin >> i;
  150.             cin >> saudades[i] >> tempo[i];
  151. //            cout << amigo << " " << i << " " << saudades[i] << " " << tempo[i] << endl;
  152.         }
  153.  
  154.         knapsack(N, T, tempo, saudades);
  155.         cout << "Amigos visitados: ";
  156.         imprimirEscolhas(N,T, tempo);
  157. //        cout<<(saudades,tempo,N,T)<<endl;
  158.  
  159.  
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement