[SPOJGCJ1C09C] Bribe the Prisoners

题意:有p个犯人,编号从1到p,现有q个犯人可以被释放,但是他两旁的所有犯人看到了觉得很不爽,所以要给1元钱来贿赂他们(请问他在监狱里到哪去花钱),一直到一个空监狱,要求安排q个犯人释放的顺序,使得总代价最小

题解:

区间dp

状态:dp[i][j]表示(i,j)的犯人被释放的最小代价

转移:dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+v[j]-v[i]-2)

初值:dp[i][i+1]=0

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #define ll long long
 8 using namespace std;
 9
10 const int N = 110;
11
12 int T,n,m,tot;
13 int v[N],dp[N][N];
14
15 int gi() {
16   int x=0,o=1; char ch=getchar();
17   while(ch!=‘-‘ && (ch<‘0‘ || ch>‘9‘)) ch=getchar();
18   if(ch==‘-‘) o=-1,ch=getchar();
19   while(ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
20   return o*x;
21 }
22
23 int main() {
24   T=gi();
25   while(T--) {
26     int p=gi(),q=gi();
27     for(int i=1; i<=q; i++) v[i]=gi();
28     v[q+1]=p+1;
29     memset(dp,63,sizeof(dp));
30     for(int i=0; i<=q; i++) dp[i][i+1]=0;
31     for(int l=1; l<=q; l++)
32       for(int i=0; i<=q+1; i++) {
33     int j=i+l+1;
34     for(int k=i+1; k<=j-1; k++) {
35       dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+v[j]-v[i]-2);
36     }
37       }
38     printf("Case #%d: %d\n", ++tot,dp[0][q+1]);
39   }
40   return 0;
41 }

总结:区间DP

这里的递推实际上是正常操作的一个逆过程

也就是相当于从子问题推出全局问题

所以要先推出范围较小一点的子区间

于是第一维枚举范围,第二维枚举起点,算出终点,第三维枚举中间点转移

时间: 2024-10-12 13:05:34

[SPOJGCJ1C09C] Bribe the Prisoners的相关文章

GCJ1C09C - Bribe the Prisoners

GCJ1C09C - 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 windo

GCJ 2009 Round 1C Bribe the Prisoners

Bribe the Prisoners no tags     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 wind

Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

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

spoj GCJ1C09C Bribe the Prisoners

题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: 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 wi

Bribe the Prisoners

题目描述 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 ne

编程题-贿赂囚犯(Bribe the prisoners)-动态规划|剪枝

转载请注明出处:http://blog.csdn.net/Lizo_Is_Me/article/details/43735509 问题描述: 一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房为止.现在要释放a1,a2,...,aQ号囚犯,如何选择释放的顺序,使得使用的金币最少. 思路: 其中很重要的一点:释放了某个囚犯以后,就把连续的牢房分成了没

spoj14846 Bribe the Prisoners

看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有罪犯全部放走的最小花费,于是我们可以将一个区间(i,j)分为(i,k-1),(k+1,j)和k这个点,表示先取走点k的人,这样这个区间就被分成了两个,于是我们便可以转移的.详见代码. 代码 #include<iostream> #include<cstdio> #include<

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co

词组习语3057组

superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1 Anointing of the Sick British English 2 Civvy Street Clerk of the Closet 3 I mean I must say 4 I suppose so I will thank you to do something 5 Incoming mail server Lithium battery 6 M