HDU 4049 Tourism Planning (Dynamic Planning)

Source: Internet
Author: User

Tourism Planning
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 1051 accepted submission (s): 460

Problem descriptionseveral 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 shocould take the tourism and when each of them shocould 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.
 
Inputthere 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.
 
Outputfor 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 1101550 55 03 230 5024 4840 7035 200 4 14 0 51 5 02 2100 10050 5050 500 2020 00 0
 
Sample output
541STAY HOME
 
Sourcethe 36th ACM/ICPC Asia Regional Beijing site -- online contest
Recommendlcy | we have carefully selected several similar problems for you: 4050 4044 4042 4048

Question:

Input description:

The first line contains two numbers, N people and M cities.

Next, M numbers indicate the cost of each person's visit to these cities.

The next n rows and M columns indicate the satisfaction of each person who visits each city.

The next n rows n list shows the additional satisfaction of the impact of each city, bij (1 <= I <= N, 1 <= j <= N ), 0 <= bij <= 1000, bij = 0 if I = J, bij = bji.

You can arrange any number of the N people to visit the M cities in sequence ~ If you quit, you cannot come back later. What is your maximum value?

Value = the satisfaction of each person who visits each city and the mutual influence between + increases the satisfaction and-the visit cost and.


Solution:

The core of this question is DP.

(1) Because n <= 10, m <= 10, the data is relatively small, you can consider the more violent approach, DP is a good brute force.

(2) It is easy to think of such a DP equation DP [Sum] [k] = max {DP [son] [k + 1]} + value [Sum] [k];

Sum indicates the people in the binary notation, and son indicates the sub-state of sum, indicating that some people in sum have left the remaining half-way,

K indicates the city to which the client is currently accessed. DP [Sum] [k] records the maximum value required in this state.

When sum and these people visited K's city, they left some people and moved to son, k + 1 city.

The cost of the transfer is the total value obtained by the sum users in the K city, which is recorded as value [Sum] [K].

(3) The only headache is the data preprocessing of value [Sum] [K], which is a violent enumeration.



Solution code:

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn=11;int n,m;//n people,m citiesint cost[maxn],a[maxn][maxn],b[maxn][maxn];int dp[(1<<maxn)][maxn],vis[(1<<maxn)][maxn],val[(1<<maxn)][maxn],marked;void input(){marked++;for(int i=0;i<m;i++) scanf("%d",&cost[i]);for(int i=0;i<n;i++){for(int j=0;j<m;j++){scanf("%d",&a[i][j]);}}for(int i=0;i<n;i++){for(int j=0;j<n;j++){scanf("%d",&b[i][j]);}}for(int i=0;i<m;i++){for(int sum=0;sum<(1<<n);sum++){val[sum][i]=0;for(int p1=0;p1<n;p1++){if( !((1<<p1)&sum) ) continue;for(int p2=0;p2<p1;p2++){if( (1<<p2)&sum ){val[sum][i]+=b[p1][p2];}}val[sum][i]+=a[p1][i]-cost[i];}}}}int DP(int sum,int k){if(k>=m) return 0;if(vis[sum][k]==marked) return dp[sum][k];int ans=0;for(int x=sum;x!=0;x=(x-1)&sum ){//for every son stateint tmp=DP(x,k+1)+val[sum][k];if(tmp>ans) ans=tmp;}vis[sum][k]=marked;return dp[sum][k]=ans;}void solve(){int ans=0;for(int i=0;i<(1<<n);i++){if(DP(i,0)>ans) ans=DP(i,0);}if(ans==0) printf("STAY HOME\n");else printf("%d\n",ans);}int main(){while(scanf("%d%d",&n,&m)!=EOF && (n||m) ){input();solve();}return 0;}


HDU 4049 Tourism Planning (Dynamic Planning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.