传送门
Tourism Planning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1115 Accepted Submission(s): 482
Problem Description
Several friends are planning to take tourism during the next holiday. They have selected some places to visit. They have decided which place to start their tourism and in which order to visit these places. However, anyone can leave halfway during the tourism and will never back to the tourism again if he or she is not interested in the following places. And anyone can choose not to attend the tourism if he or she is not interested in any of the places.
Each place they visited will cost every person certain amount of money. And each person has a positive value for each place, representing his or her interest in this place. To make things more complicated, if two friends visited a place together, they will get a non negative bonus because they enjoyed each other’s companion. If more than two friends visited a place together, the total bonus will be the sum of each pair of friends’ bonuses.
Your task is to decide which people should take the tourism and when each of them should leave so that the sum of the interest plus the sum of the bonuses minus the total costs is the largest. If you can’t find a plan that have a result larger than 0, just tell them to STAY HOME.
Each place they visited will cost every person certain amount of money. And each person has a positive value for each place, representing his or her interest in this place. To make things more complicated, if two friends visited a place together, they will get a non negative bonus because they enjoyed each other’s companion. If more than two friends visited a place together, the total bonus will be the sum of each pair of friends’ bonuses.
Your task is to decide which people should take the tourism and when each of them should leave so that the sum of the interest plus the sum of the bonuses minus the total costs is the largest. If you can’t find a plan that have a result larger than 0, just tell them to STAY HOME.
Input
There are several cases. Each case starts with a line containing two numbers N and M ( 1<=N<=10, 1<=M<=10). N is the number of friends and M is the number of places. The next line will contain M integers Pi (1<=i<=M) , 1<=Pi<=1000, representing how much it costs for one person to visit the ith place. Then N line follows, and each line contains M integers Vij (1<=i<=N, 1<=j<=M), 1<=Vij<=1000, representing how much the ith person is interested in the jth place. Then N line follows, and each line contains N integers Bij (1<=i<=N, 1<=j<=N), 0<=Bij<=1000, Bij=0 if i=j, Bij=Bji.
A case starting with 0 0 indicates the end of input and you needn’t give an output.
A case starting with 0 0 indicates the end of input and you needn’t give an output.
Output
For each case, if you can arrange a plan lead to a positive result, output the result in one line, otherwise, output STAY HOME in one line.
Sample Input
2 1
10
15
5
0 5
5 0
3 2
30 50
24 48
40 70
35 20
0 4 1
4 0 5
1 5 0
2 2
100 100
50 50
50 50
0 20
20 0
0 0
Sample Output
5
41
STAY HOME
Source
Recommend
13075053 | 2015-03-09 18:32:49 | Accepted | 4049 | 405MS | 2120K | 3076 B | G++ | czy |
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <string> 7 #define N 15 8 9 using namespace std; 10 11 int n,m; 12 int p[N]; 13 int v[N][N]; 14 int b[N][N]; 15 int dp[N][ (1<<10) ]; 16 int ans; 17 int tot; 18 int happy[N][ (1<<10) ]; 19 20 vector<int> can[ (1<<10) ]; 21 22 int cal(int i,int o); 23 int ok(int k,int o); 24 25 void ini() 26 { 27 int i,j; 28 ans=0; 29 memset(dp,0,sizeof(dp)); 30 for(i=1;i<=m;i++){ 31 scanf("%d",&p[i]); 32 } 33 for(i=0;i<n;i++){ 34 for(j=1;j<=m;j++){ 35 scanf("%d",&v[i][j]); 36 } 37 } 38 for(i=0;i<n;i++){ 39 for(j=0;j<n;j++){ 40 scanf("%d",&b[i][j]); 41 } 42 } 43 int o; 44 tot = (1<<n); 45 for(i=1;i<=m;i++){ 46 for(o=0;o<tot;o++){ 47 dp[i][o]=-1000000000; 48 } 49 } 50 //printf(" n=%d m=%d tot=%d ",n,m,tot ); 51 for(i=1;i<=m;i++){ 52 for(o=0;o<tot;o++){ 53 happy[i][o]=cal(i,o); 54 } 55 } 56 57 for(o=0;o<tot;o++){ 58 can[o].clear(); 59 for(int k=0;k<tot;k++){ 60 if(ok(k,o)==1){ 61 can[o].push_back(k); 62 } 63 } 64 } 65 } 66 67 int cal(int i,int o) 68 { 69 int re=0; 70 int j,k; 71 int cc=0; 72 //printf(" i=%d o=%d ",i,o ); 73 for(j=0;j<n;j++){ 74 if( (1<<j) & o ){ 75 cc++; 76 re+=v[j][i]; 77 } 78 } 79 //printf(" 1 re=%d ",re ); 80 for(j=0;j<n;j++){ 81 if( (1<<j) & o ){ 82 for(k=j+1;k<n;k++){ 83 if( (1<<k) & o ){ 84 re += b[j][k]; 85 } 86 } 87 } 88 } 89 // printf(" 2 re=%d ",re ); 90 re -= p[i]*cc; 91 //printf(" 3 re=%d ",re ); 92 //printf(" i=%d o=%d re=%d ",i,o,re ); 93 return re; 94 } 95 96 int ok(int k,int o){ 97 int j; 98 for(j=0;j<n;j++){ 99 // printf(" j=%d ",j ); 100 if( (1<<j) & o ){ 101 if( ( (1<<j) &k ) ==0 ){ 102 return 0; 103 } 104 } 105 } 106 return 1; 107 } 108 109 void solve() 110 { 111 int o,j,i,k; 112 int te; 113 for(i=1;i<=m;i++){ 114 //printf(" i=%d ",i ); 115 for(o=0;o<tot;o++){ 116 // printf(" o=%d ", o); 117 for(vector<int>::iterator it =can[o].begin();it != can[o].end();it++){ 118 // for(k=0;k<tot;k++){ 119 //printf(" k=%d ", k); 120 k=*it; 121 // if(ok(k,o)==0) continue; 122 123 //te=cal(i,o); 124 te=happy[i][o]; 125 dp[i][o]=max(dp[i][o],dp[i-1][k]+te); 126 //printf(" i=%d o=%d dp=%d ", i,o,dp[i][o]); 127 } 128 } 129 } 130 131 i=m; 132 for(o=0;o<tot;o++){ 133 //printf(" o=%d dp=%d ",o,dp[m][o] ); 134 ans=max(ans,dp[m][o]); 135 } 136 } 137 138 void out() 139 { 140 if(ans<=0){ 141 printf("STAY HOME "); 142 } 143 else{ 144 printf("%d ", ans); 145 } 146 } 147 148 int main() 149 { 150 while(scanf("%d%d",&n,&m)!=EOF){ 151 if(n==0 && m==0) break; 152 ini(); 153 solve(); 154 out(); 155 } 156 }