贿赂囚犯 Bribe the prisoners (动态规划)

问题描述:

Problem
In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called “neighbours.” A wall with a window separates adjacent cells, and neighbours can communicate through that window.
All prisoners live in peace until a prisoner is released. When that happens, the released prisoner’s neighbours find out, and each communicates this to his other neighbour. That prisoner passes it on to his other neighbour, and so on until they reach a prisoner with no other neighbour (because he is in cell 1, or in cell P, or the other adjacent cell is empty). A prisoner who discovers that another prisoner has been released will angrily break everything in his cell, unless he is bribed with a gold coin. So, after releasing a prisoner in cell A, all prisoners housed on either side of cell A - until cell 1, cell P or an empty cell - need to be bribed.
Assume that each prison cell is initially occupied by exactly one prisoner, and that only one prisoner can be released per day. Given the list of Q prisoners to be released in Q days, find the minimum total number of gold coins needed as bribes if the prisoners may be released in any order.
Note that each bribe only has an effect for one day. If a prisoner who was bribed yesterday hears about another released prisoner today, then he needs to be bribed again.
Input
The first line of input gives the number of cases, N. N test cases follow. Each case consists of 2 lines. The first line is formatted as
P Q
where P is the number of prison cells and Q is the number of prisoners to be released.
This will be followed by a line with Q distinct cell numbers (of the prisoners to be released), space separated, sorted in ascending order.
Output
For each test case, output one line in the format
Case #X: C
where X is the case number, starting from 1, and C is the minimum number of gold coins needed as bribes.
Limits
1 ≤ N ≤ 100
Q ≤ P
Each cell number is between 1 and P, inclusive.
Large dataset
1 ≤ P ≤ 10000
1 ≤ Q ≤ 100
Sample
Input
2
8 1
3
20 3
3 6 14
Output
Case #1: 7
Case #2: 35
Note
In the second sample case, you first release the person in cell 14, then cell 6, then cell 3. The number of gold coins needed is 19 + 12 + 4 = 35. If you instead release the person in cell 6 first, the cost will be 19 + 4 + 13 = 36.

题目大意:
有p个囚犯在监狱,要放出q个囚犯,给出将要释放的囚犯编号(代码中我们使用a[1]~a[p]来存放编号),每放出一个人,他周围的人(两边连续的直到碰到空的监狱或者尽头)都要贿赂1枚金币,目前释放的顺序是不确定的,问最少花费多少金币。
解题思路:
释放某个囚犯后,就把连续的牢房分成了两段,此后这两段牢房就相互独立了。
在这里插入图片描述
可以将释放的过程近似看作一棵二叉树(注意每个非叶结点都应计算一次花费),这样就可以把问题视为构造出一棵具有最小花费的二叉树。从小的子树出发,不断扩大子树的规模,即根据最小的子问题组合成稍大一点的子问题(动态规划)。
a[i]代表第i个要释放的囚犯的编号,为了方便处理边界问题,我们把牢房的左墙壁和右墙壁也当作要释放的囚犯。
dp[i][j]表示释放a[i]和a[j]之间应该要释放的囚犯所需要的最少金币数。
初始化:dp[i][i+1]=0(因为a[i]和a[i+1]之间没有待释放囚犯)
动态方程:dp[ i ][ j ] = min( dp[ i ][ j ], dp[ i ][ k ]+dp[ k ][ j ] ) + a[ j ] - a[i] - 2
(a[k]为a[i]和a[j]之间的一名待释放囚犯)

AC代码

#include<iostream>
#include<algorithm>
using namespace std;
int dp[105][105];//ans is dp[0][q+1]
int a[105];
#define INF 0x3fffffff
int main() {
	int t;
	cin >> t;
	int cnt = 0;
	while (t--) {
		cnt++;
		int p, q;
		cin >> p >> q;
		//方便解决边界问题  左边界为要释放的第0个囚犯
		//右边界为要释放的第q+1个囚犯
		a[0] = 0;
		a[q + 1] = p + 1;
		//输入要释放的囚犯
		for (int i = 1; i <= q; i++) {
			cin >> a[i];
		}
		//dp[i][j]即全部释放a[i]与a[j]囚犯之间要释放的囚犯后,所花费的最小金币
		//注意不包含边界
		//dp[0][q+1]即为答案
		for (int i = 0; i <= q; i++) {
			dp[i][i + 1] = 0;
		}
		//w是跨度 上一步已经对跨度1进行了初始化
		for (int w = 2; w <= q + 1; w++) {
			for (int i = 0; i + w <= q + 1; i++) {
				//j是本次区间的最右端
				int j = i + w;
				int tmp = INF;
				for (int k = i + 1; k < j; k++) {
					tmp = min(tmp, dp[i][k] + dp[k][j]);
				}
				//用于贿赂本区间内的囚犯
				dp[i][j] = tmp + a[j] - a[i] - 2;
			}
		}
		cout << "Case #" << cnt << ": ";
		cout << dp[0][q + 1] << endl;
	}
	//system("pause");
	return 0;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值