dp问题最佳路径的构建 poj 1015

来源:互联网 发布:2017淘宝虚假交易规则 编辑:程序博客网 时间:2024/06/05 16:02

我承认,作为一个新手,这道题困扰了我很长时间,各个步骤我觉得都很难。从最优解的刻画到最后最佳方案的构建都花了我很多时间,也听了许多大神的建议。最后终于解决了这道题。

Jury Compromise
Description
In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is done as follows. First, several people are drawn randomly from the public. For each person in this pool, defence and prosecution assign a grade from 0 to 20 indicating their preference for this person. 0 means total dislike, 20 on the other hand means that this person is considered ideally suited for the jury. 
Based on the grades of the two parties, the judge selects the jury. In order to ensure a fair trial, the tendencies of the jury to favour either defence or prosecution should be as balanced as possible. The jury therefore has to be chosen in a way that is satisfactory to both parties. 
We will now make this more precise: given a pool of n potential jurors and two values di (the defence's value) and pi (the prosecution's value) for each potential juror i, you are to select a jury of m persons. If J is a subset of {1,..., n} with m elements, then D(J ) = sum(dk) k belong to J 
and P(J) = sum(pk) k belong to J are the total values of this jury for defence and prosecution. 
For an optimal jury J , the value |D(J) - P(J)| must be minimal. If there are several jurys with minimal |D(J) - P(J)|, one which maximizes D(J) + P(J) should be selected since the jury should be as ideal as possible for both parties. 
You are to write a program that implements this jury selection process and chooses an optimal jury given a set of candidates.

Input

The input file contains several jury selection rounds. Each round starts with a line containing two integers n and m. n is the number of candidates and m the number of jury members. 
These values will satisfy 1<=n<=200, 1<=m<=20 and of course m<=n. The following n lines contain the two integers pi and di for i = 1,...,n. A blank line separates each round from the next. 
The file ends with a round that has n = m = 0.

Output

For each round output a line containing the number of the jury selection round ('Jury #1', 'Jury #2', etc.). 
On the next line print the values D(J ) and P (J ) of your jury as shown below and on another line print the numbers of the m chosen candidates in ascending order. Output a blank before each individual candidate number. 
Output an empty line after each test case.

Sample Input

4 2 1 2 2 3 4 1 6 2 0 0 

Sample Output

Jury #1 Best jury has value 6 for prosecution and value 4 for defence:  2 3 
题目的意思是,有n个候选人,要得到m个人组成的陪审团。要求是陪审团总体较为中立。一开始我的想法是建立一个dp[i][j],表示从前i个人中选择j个人得到的最优解,再依次得到结果。但这样就会出现问题,由于这样的程序就会尽量选择较为中立的那个人,极端情况就会被忽略。后来,我想到,dp[i][j]表示的是选择了i个人之后得到的总中立值。因为最优解一定有最大的总值和中立值,一下便解决了这个问题,也减少了运行的时间。

但是,如何构建最优解呢?我们另外起一个相同的数组表示在每一次选择时所选择的候选人,为了防止重复,在每次选择的时候我们都进行回溯以确定这个候选人没有被选中。然后在确定最后结果后我们按次将选择的候选人储存到另外一个数组中去。再进行排序,就可以得到最后的结果了。

这道题,值得我反复的品味!!!

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct candi{int d, p, sum, differ;};candi mem[202];int select[22],mid;int n,m,num=0,sumd,sump,dp[22][805],choose[22][805];bool selecting(int i, int j, int k){while (i > 0 && choose[i][k] != j){k -= mem[choose[i][k]].differ;i--;}if (i == 0)return true;else return false;}void process(){int i, j, k;dp[0][mid] = 0;for (i = 1; i <= m; i++)for (j = 0; j <= 2 * mid; j++){if (dp[i - 1][j] >= 0)for (k = 1; k <= n; k++){if (dp[i - 1][j] + mem[k].sum > dp[i][j + mem[j].differ] && selecting(i - 1, k, j)){dp[i][j + mem[k].differ] = dp[i - 1][j] + mem[k].sum;choose[i][j + mem[k].differ] = k;}}}for (k = 0; k <= mid; k++)if (dp[m][mid - k] >= 0 || dp[m][mid + k] >= 0)break;int div = dp[m][mid - k] > dp[m][mid + k] ? (mid - k) : (mid + k);for (i = 1,k=div,j=m; i <= m; i++){sump += mem[choose[j][k]].p;sumd += mem[choose[j][k]].d;select[j] = choose[j][k];k -= mem[choose[j][k]].differ;j--;}cout << "Jury #" << num << endl;cout << "Best jury has value " << sump << " for prosecution and value " << sumd << " for defence:" << endl;sort(select+1, select + m+1);for (i = 1; i <= m; i++){printf(" %d", select[i]);if (i == m)cout << endl;}}int main(){int i;while (scanf_s("%d %d", &n, &m) != EOF){if (!n&&!m)break;num++;memset(dp, -800, sizeof(dp));   //负值即表示无法得到这个结果memset(select, -1, sizeof(select));sump = sumd = 0;mid = 20 * m;            //挪移起点,保证在计算时中立值为正for (i = 1; i <= n; i++){cin >> mem[i].p >> mem[i].d;mem[i].differ = mem[i].p - mem[i].d;mem[i].sum = mem[i].d + mem[i].p;}process();}return 0;}


0 0
原创粉丝点击