POJ 1015-Jury Compromise动态规划

题目来源:http://poj.org/problem?id=1015

解题报告:

f[i][j]: 代表i个人,sum(DJ) - sum(PJ)为j-400 的组合中,最大的sum(DJ) + sum(PJ)值

path[i][j]:代表路径中的最后一人的编号。

d[k] = Dj[k] - Pj[k]

s[k] = Dj[k] + Pj[k]

那么有递推式:

f[i][j + d[k]] = max(f[i][j + d[k]], f[i-1][j] + s[k])

还要保证K没有出现在f[i-1][j]之前的路径中,这个可以通过

pre = path[i-1][j]

path[i-2][j-d[pre]]这样依次向前查找。

如果f[i][j + d[k]]的值更新了,则同时要更新path[i][j + d[k]] = k。


最后根据f[][]的值,从400开始向两边搜索,找到有值的f[i][j],然后根据path[i][j]依次向前获得总路径,用sort()排序,输出结果。


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

#define abs(a) ((a)>0?(a):(-a))
#define INFINITE -100000

int path[21][802];
int f[21][802];

int main()
{
	int m, n;
	int caseNum=1;
	while(1)
	{
		cin >> n;
		cin >> m;
		if(m==0 && n==0)
			break;
		int *di=new int [n];
		int *pi=new int [n];
		int *s = new int[n];
		int *d = new int[n];
		for(int i=0;i<n;i++)
		{
			cin >> di[i] >> pi[i];
			s[i] = di[i] + pi[i];
			d[i] = di[i] - pi[i];
		}

		for (int i = 0; i <= 20; i++)
		{
			for (int j = 0; j <= 800; j ++)
			{
				f[i][j] = INFINITE;
				path[i][j] = -1;
			}
		}

		for (int k = 0; k < n; k++)
		{
			if (f[1][d[k]+400] < s[k])  //一开始这里没判断,WA了很多次
			{
				f[1][d[k] + 400] = s[k];
				path[1][d[k] + 400] = k;	
			}
		}

		for (int i = 1; i < m; i++)
		{
			for (int j = 0; j <= 800; j++)
			{
				if (f[i][j] >= 0)
				{
					for (int k = 0; k < n; k++)
					{
						bool existed = false;
						if (path[i][j] == k)
							continue;
						int pre = path[i][j];
						int begin = j;
						for (int l = i-1; l >= 1; l--)
						{
							begin = begin - d[pre];
							pre = path[l][begin];
							if(pre == k)
							{
								existed = true;
								break;
							}
						}
						if (!existed)
						{
							if (f[i+1][j+d[k]] < f[i][j] + s[k])
							{
								f[i+1][j+d[k]] = f[i][j] + s[k];
								path[i+1][j+d[k]] = k;
							}
						}
					}				
				}
			}
		}

		int begin;

		for (int i = 0; i <= 400; i++)
		{
			if (f[m][400-i] != INFINITE || f[m][400+i] != INFINITE)
			{
				if ( f[m][400-i] < f[m][400+i] )
					begin = 400 + i;
				else
					begin = 400 - i;
				break;
			}
		}

		vector<int> finalPath;
		int pro = 0;
		int def = 0;
		int pre = path[m][begin];
		
		for (int i = 0; i < m; i++)
		{
			finalPath.push_back(pre);
			pro += di[pre];
			def += pi[pre];
			begin = begin - d[pre];
			pre = path[m-i-1][begin];
		}

		cout << "Jury #" << caseNum++ << endl;
		cout << "Best jury has value " << pro << " for prosecution and value "
			<< def << " for defence:" << endl;
		sort(finalPath.begin(), finalPath.end());
		for (int i = 0; i < finalPath.size(); i++)
		{
			cout << " " << finalPath[i] + 1;
		}
		cout << endl;
		delete[] di;
		delete[] pi;
		delete[] s;
		delete[] d;
		
	}
}

附录:

Jury Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21381 Accepted: 5458 Special Judge

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 

Hint

If your solution is based on an inefficient algorithm, it may not execute in the allotted time.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值