tcbpg

Untitled

Aug 25th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. //UVa 10817 Headmaster's Headache
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. #define forn(i, n) for(int i = 0; i < (int) (n); i++)
  11. #define forsn(i, s, n) for(int i = (s); i < (int) (n); i++)
  12.  
  13. typedef long long tint;
  14.  
  15. int s, m, n;
  16. tint dp[110][(1 << 8)][(1 << 8)]; //Minimo costo si me quedan cubrir los puestos i con dos docentes y los puestos j con un docente
  17.  
  18. const int SIZE = 110;
  19. const int INF = 10000000;
  20.  
  21. int servcost[SIZE];
  22. int servmats[SIZE];
  23. int  appcost[SIZE];
  24. int  appmats[SIZE];
  25.  
  26. tint calc(int p, int onet, int twot){
  27.     if(twot == (1 << s) - 1) return 0;
  28.     if(p == n) return INF;
  29.  
  30.     if(dp[p][onet][twot] == -1){
  31.         dp[p][onet][twot] =
  32.                 min(calc(p+1,onet ^ appmats[p], twot | (onet & appmats[p]))+appcost[p],
  33.                         calc(p+1, onet, twot));
  34.     }
  35.  
  36.     return dp[p][onet][twot];
  37. }
  38.  
  39. int main(){
  40. #ifndef ONLINE_JUDGE
  41.     freopen("test.in", "r", stdin);
  42. #endif
  43.  
  44.     while(scanf("%d %d %d\n", &s, &m, &n) && s != 0){
  45.         forn(i, m){
  46.             cin >> servcost[i];
  47.             string s; getline(cin, s);
  48.             istringstream ss(s);
  49.  
  50.             int materia, mask = 0;
  51.             while(ss >> materia) mask = mask | (1 << (materia-1));
  52.  
  53.             servmats[i] = mask;
  54.         }
  55.  
  56.         forn(i, n){
  57.             cin >> appcost[i];
  58.  
  59.             string s; getline(cin, s);
  60.             istringstream ss(s);
  61.  
  62.             int materia, mask = 0;
  63.             while(ss >> materia) mask = mask | (1 << (materia-1));
  64.  
  65.             appmats[i] = mask;
  66.         }
  67.  
  68.         int oneteach = 0, twoteach = 0;
  69.         forn(i, n) forn(j, 1 << s) forn(k, 1 << s) dp[i][j][k] = -1;
  70.  
  71.         forn(i, m){
  72.             twoteach |= (oneteach & servmats[i]);
  73.             oneteach = oneteach ^ servmats[i];
  74.         }
  75.  
  76.         int tot = 0; forn(i, m) tot+=servcost[i];
  77.  
  78.         cout << calc(0, oneteach, twoteach)+tot << endl;
  79.     }
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment