Advertisement
Kaidul

A

Nov 17th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int Set(int N, int pos){
  6.     return N = N | (1 << pos);
  7. }
  8. bool Check(int N, int pos) {
  9.     return (bool)(N & (1 << pos));
  10. }
  11.  
  12. #define Max 12
  13. vector < int > adj[Max];
  14. int N, M;
  15. int co[Max], wl[Max];
  16. int dp[Max][1 << (Max - 1)][1 << (Max - 1)];
  17.  
  18. int tsp(int u, int mask, int mask2) {
  19.     if(dp[u][mask][mask2] != -1)
  20.         return dp[u][mask][mask2];
  21.     int ans = 0;
  22.     for(int k = 0; k < (int)adj[u].size(); k++) {
  23.         int v = adj[u][k];
  24.         if(wl[v] <= 0)
  25.             continue;
  26.         bool x = Check(mask, v);
  27.         bool y = Check(mask2, v);
  28.         if(wl[v] == 2) {
  29.             if( x and y ) {
  30.                 continue;
  31.             } else if( x and !y ) {
  32.                 ans = max(ans, tsp(v, mask, Set(mask2,v)));
  33.             } else {
  34.                 ans = max(ans, tsp(v, Set(mask, v), mask2) + co[v]);
  35.             }
  36.         } else {
  37.             if( !x ) {
  38.                 ans  = max(ans, tsp(v, Set(mask, v), mask2) + co[v]);
  39.             }
  40.  
  41.         }
  42.  
  43.     }
  44.     return dp[u][mask][mask2] = ans;
  45.  
  46. }
  47. int main() {
  48. #ifndef ONLINE_JUDGE
  49.     freopen("input.txt", "r", stdin);
  50. #endif
  51.     int u, v;
  52.     int mask, mask2;
  53.     int tcase, caseNo = 0;
  54.     scanf("%d", &tcase);
  55.     while( tcase-- ) {
  56.         scanf("%d %d", &N, &M);
  57.         for(int i = 1; i <= N; i++ )
  58.             scanf("%d", &wl[i]);
  59.         for(int i = 1; i <= N; i++ )
  60.             scanf("%d", &co[i]);
  61.         for(int i = 1; i <= M; i++ ) {
  62.             scanf("%d %d", &u, &v);
  63.             adj[u].push_back(v);
  64.             adj[v].push_back(u);
  65.         }
  66.         int ret = 0;
  67.         memset(dp, -1, sizeof(dp));
  68.         for(int i = 1; i <= N; i++) {
  69.             mask = mask2 = 0;
  70.             if( wl[i] > 0) {
  71.                 ret = max(ret, tsp(i, Set(mask, i), mask2) + co[i]);
  72.             }
  73.         }
  74.         printf("Case %d: %d\n", ++caseNo, ret);
  75.         for(int i  = 0; i < Max; i++)
  76.             adj[i].clear();
  77.     }
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement