一道经典动归题 (题意有点难理解~)
由于不满足四边形不等式 于是只能在o(n3)的时间内解决了
(小弟不才~呵呵~如果你有好的方法 直接告诉我吧~)
Solution:
#include <stdio.h>
#include <string.h>
int ntc;
int N;
double F, ans;
int land[420];
double s[420][420];
int w[420][420];
void init()
{
//initiation
memset(s, 0, sizeof(s));
memset(w, 0, sizeof(w));
ans = 10e10;
//input
int i;
for(i=0; i<N; i++)
scanf("%d", &land[i]);
memcpy(land+N, land, N*(sizeof(land[0])));
//pretreatment
for(i=0; i<2*N; i++)
w[i][i] = land[i];
}
void cal_w()
{
int i, j, l;
for(l=2; l<=N; l++)
for(i=0; i<=2*N-l; i++)
{
j = i+l-1;
w[i][j] = w[i][j-1] + w[j][j];
}
}
void cal_s()
{
int i, j, l, k;
for(l=2; l<=N; l++)
for(i=0; i<=2*N-l; i++)
{
j = i+l-1;
double min = 10e10, temp;
for(k=i; k<j; k++)
{
temp = s[i][k] + s[k+1][j] + F*(w[i][k] > w[k+1][j] ? w[i][k] : w[k+1][j]);
if(temp < min) min = temp;
}
s[i][j] = min;
}
}
void work()
{
cal_w();
cal_s();
int i;
double min = 10e10;
for(i=0; i<N; i++)
if(s[i][i+N-1]<min) min = s[i][i+N-1];
ans = min;
}
void output()
{
printf("%.2lf\n", ans);
}
int main()
{
//freopen("t.in", "r", stdin);
while(scanf("%d%lf",&N, &F), N>0)
{
init();
work();
output();
}
return 0;
}
Land Division Tax
Time Limit:5000MS Memory Limit:30000K
Total Submit:365 Accepted:162
Description
International Concrete Projects Company (ICPC) is a construction company which specializes in building houses for the high-end market. ICPC is planning a housing development for new homes around a lake. The houses will be built in lots of different sizes, but all lots will be on the lake shore. Additionally, every lot will have exactly two neighbors in the housing development: one to the left and one to the right.
ICPC owns the land around the lake and needs to divide it into lots according to the housing development plan. However, the County Council has a curious regulation regarding land tax, intended to discourage the creation of small lots:
- land can only be divided using a sequence of land divisions;
- a land division is an operation that divides one piece of land into two pieces of land; and
- for each land division, a land division tax must be paid.
Denoting by A the area of the largest resulting part of the division, the value of the land division tax is A*F, where F is the division tax factor set yearly by the County Council. Note that due to (2), in order to divide a piece of land into N lots, N - 1 land divisions must be performed, and therefore N - 1 payments must be made to the County Council.
For example, considering the figure above, if the division tax factor is 2.5 and the first land division separates the lot of 500 units of area from the other lots, the land division tax to be paid for this first division is 2.5 * (300 + 200 + 100 + 100 + 100). If the next land division separates the lot of 300 units together with its neighbor lot of 100 units, from the set of the remaining lots, an additional 2.5 * (300 + 100) must be paid in taxes, and so on. Note also that some land divisions are not possible, due to (2). For example, after the first land division mentioned above, it is not possible to make a land division to separate the lot of 300 units together with the lot of 200 units from the remaining three lots, because more than two parts would result from that operation.
Given the areas of all lots around the lake and the current value of the division tax factor, you must write a program to determine the smallest total land division tax that should be paid to divide the land according to the housing development plan.
Input
The input contains several test cases. The first line of a test case contains an integer N and a real F, indicating respectively the number of lots (1 <= N <= 200) and the land division tax factor (with precision of two decimal digits, 0 < F <= 5.00). The second line of a test case contains N integers Xi, representing the areas of contiguous lots in the development plan (0 < Xi <= 500, for 1 <= i <= N); furthermore, Xk is neighbour to Xk+1 for 1 <= k <= N - 1, and XN is neighbour to X1. The end of input is indicated by N = F = 0.
Output
For each test case in the input your program must produce a single line of output, containing the minimum total land division tax, as a real number with precision of two decimal digits.
Sample Input
4 1.50 2 1 4 1 6 2.50 300 100 500 100 100 200 0 0
Sample Output
13.50 4500.00
Source
South America 2004