背包(4)Hdu 1520 Anniversary party(树形背包)+ Poj 1463 Strategic game(还可以贪心来做)

来源:互联网 发布:怎么定义软件控制策略 编辑:程序博客网 时间:2024/05/09 09:13

Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2779    Accepted Submission(s): 1239


Problem Description
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.
 

Input
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form: 
L K 
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line 
0 0
 

Output
Output should contain the maximal sum of guests' ratings.
 

Sample Input
711111111 32 36 47 44 53 50 0
 

Sample Output
5
 

Source
Ural State University Internal Contest October'2000 Students Session
 

Recommend
linle
 


 

 

/* 题意:新年晚会上,要邀请公司人员会餐,每个人有个happy值,要求是一个人和他的直属上司不能同时到场,人数不限,求能得到的最大happy值。分析:用dp[][1]表示到场,dp[][0]表示不到场,通过关系树,在树上跑背包,取较大值输出。*/#include<cstdio>#include<vector>#include<string.h>using namespace std;int happy[6005],visit[6005];int dp[6005][2];vector<int>r[6005];//存边int max(int x,int y){return x>y?x:y;}void dfs(int x){visit[x] = 1;int u;for (int i = 0; i < r[x].size(); i++)//找 x 的下级{u = r[x][i];//x 的下级if(!visit[u]){dfs(u);dp[x][1] += dp[u][0];//如果x参加了,那么x的下级u就都不能参加dp[x][0] += max(dp[u][1],dp[u][0]);//如果x没参加,那么u可以参加,也可以不参加,取较大值。}}}int main(){int n,l,k;while(~scanf("%d",&n)){memset(visit,0,sizeof(visit));for (int i = 1; i <= n; i++){scanf("%d",&happy[i]);r[i].clear();//容易坑的地方啊!!!!!一定要记得clear。dp[i][0] = 0;//初始化dp[i][1] = happy[i];}while(~scanf("%d %d",&l,&k)){if(l==0 && k==0)  break;r[k].push_back(l);//存双向边r[l].push_back(k);}dfs(1);printf("%d\n",max(dp[1][1],dp[1][0]));//取较大值输出}return 0;}



个人比较习惯用vector来记录每个结点的下级节点,当然也可以用邻接表等其他方法来做。

 

还有几道极为相似的题目,比如Poj 1463

Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 5480 Accepted: 2480

Description

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

For example for the tree:

the solution is one soldier ( at the node 1).

Input

The input contains several data sets in text format. Each data set represents a tree with the following description:

  • the number of nodes
  • the description of each node in the following format
    node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads
    or
    node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

Output

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

Sample Input

40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)

Sample Output

12

Source

Southeastern Europe 2000

 

#include<cstdio>#include<iostream>#include<algorithm>#include<vector>#include<cstring>using namespace std;#define MAX 1510bool visit[MAX];int dp[MAX][2], n,m;int p,s;int min(int a,int b){    return a<b ? a:b;}vector<int> v[MAX];//存与当前点直接相连的其他点。void dfs(int u){visit[u] = true;for(unsigned i=0;i<v[u].size();i++){        int x=v[u][i];if(!visit[x]){dfs(x);dp[u][1]+=min(dp[x][0],dp[x][1]);//dp[x][1]和dp[x][0]分别表示x这个地方放不放。dp[u][0]+=dp[x][1];//如果它的后继节点放了,那么这个点可以放也可以不放,如果没放,那这个点一定要放。}    }}int main(){//freopen("D:in.txt","r",stdin);//freopen("D:out.txt","w",stdout);    int i;    while(scanf("%d",&n)!=EOF){if(n==1){scanf("%d:(%d)",&p,&m);printf("1\n");continue;}else{memset(visit,false,sizeof(visit));for(i=0;i<n;i++){v[i].clear();dp[i][0]=0;dp[i][1]=1;        }        for(i=0;i<n;i++){            scanf("%d:(%d)",&p,&m);//注意数据的输入。            while(m--){                scanf("%d",&s);                v[p].push_back(s);//存了双向的v[s].push_back(p);            }        }dfs(0);        printf("%d\n",min(dp[0][0],dp[0][1]));    }}    return 0;}


 特别推荐:这道题还可以用贪心来做。

贪心策略:每次找叶子结点,在其父亲结点放上士兵,删边,重复上述过程。

代码:

#include<cstdio>#include<iostream>#include<algorithm>#include<vector>#include<cstring>using namespace std;#define MAX 1510int n,m;int p,s;vector<int> v[MAX];//存与当前点直接相连的其他点。int deg[MAX];bool vis[MAX];int Greedy(){int ans = 0,num = 0,soilder,t;while(num < n){//num:已看守的结点for (int i = 0; i < n; i++){if(deg[i] == 1 && !vis[i]){//找到一个叶子结点for (unsigned j = 0; j < v[i].size(); j++){t = v[i][j];if(!vis[t]){soilder = t;break;}}vis[soilder] = true;ans++;num++;for (unsigned j = 0; j < v[soilder].size(); j++){t = v[soilder][j];deg[t]--;if(!vis[t] && deg[t] == 0){//printf("%d ",t);num ++; vis[t] = true;}}#if 0printf("\nleaf: %d  soilder : %d num: %d\n\n",i,soilder,num);#endif//break;}}}return ans;}int main(){//freopen("D:in.txt","r",stdin);//freopen("D:out2.txt","w",stdout);    int i;    while(scanf("%d",&n)!=EOF){if(n==1){scanf("%d:(%d)",&p,&m);printf("1\n");continue;}else{memset(deg,0,sizeof(deg));memset(vis,false,sizeof(vis));for(i=0;i<n;i++)v[i].clear();for(i=0;i<n;i++){scanf("%d:(%d)",&p,&m);while(m--){scanf("%d",&s);v[p].push_back(s);deg[p]++;v[s].push_back(p);deg[s]++;}}printf("%d\n",Greedy());}}    return 0;}


原创粉丝点击