Problem Description
Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Bananas Day. But the little monkeys don’t know each other, so as the king, SDH must do something.
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are:
1.every time, he can only introduce one monkey and one of this monkey’s neighbor.
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows;
3.each little monkey knows himself;
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing.
Input
There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), which is the number of monkeys. The next line contains n positive integers(less than 1000), means the making friends time(in order, the first one and the last one are neighbors). The input is end of file.
Output
For each case, you should print a line giving the mininal time SDH needs on introducing.
Sample Input
8
5 2 4 7 6 1 3 9
Sample Output
105
题意:
一群猴子围成圈,每个猴子互相不认识,猴王要给大家互相认识,每个猴子认识别人需要一个时间花费,而且A猴子认识B猴子,则A猴子认识的所有猴子和B猴子认识的所有猴子都能认识,这个代价为所有AB猴子认识的猴子的时间花费和。
分析:
- 区间DP模板题
- 不优化会超时
- !!! INF定义为0x3f3f3f时会wa掉(
劳资一年多都这么用的也没啥错啊)/(ㄒoㄒ)/~~
四边形不等式:
若f[i][j]=min{f[i][k]+f[k+1][j]+w[i][j]} ,i<=k<=j;
s[i][j]为使f[i][j]取到最小值的k ,其中有(a<=b<=c<=d)
1.w[b][c]<=w[a][d] (w满足区间包含单调性)
2.w[a][c]+w[b][d]<=w[b][c]+w[a][d] (w满足四边形不等式)
则f也满足四边形不等式,所以 s[i][j-1]<=s[i][j]<=s[i+1][j]
AC-Code:
#include <iostream>
#include <vector>
#include <utility>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <fstream>
#include <set>
using namespace std;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
const int INF = 0x7fffffff;
const int MAXN = 1005 * 2;
int dp[MAXN][MAXN];// 合并第i->j堆最小代价
int a[MAXN];
int cost[MAXN];//前缀和cost(a, b) = cost[b] - cost[a-1]
int s[MAXN][MAXN];
int main() {
ios;
int n;
while (cin >> n) {
memset(dp, INF, sizeof dp);
cost[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
cost[i] = cost[i - 1] + a[i];
dp[i][i] = 0;
s[i][i] = i;
}
for (int i = 1; i < n; i++) {
cost[i + n] = cost[i + n - 1] + a[i];
dp[i + n][i + n] = 0;
s[i + n][i + n] = i + n;
}
for (int len = 2; len <= n; len++)
for (int i = 1; i <= 2 * n - 1; i++) {
int j = i + len - 1;
if (j > 2 * n - 1)
break;
for (int k = s[i][j - 1]; k <= s[i + 1][j]; k++) {
if (dp[i][j] > dp[i][k] + dp[k + 1][j] + cost[j] - cost[i - 1]) {
dp[i][j] = dp[i][k] + dp[k + 1][j] + cost[j] - cost[i - 1];
s[i][j] = k;
}
}
}
int ans = INF;
for (int i = 1; i <= n; i++)
ans = min(ans, dp[i][i + n - 1]);
cout << ans << endl;
}
return 0;
}