TYVJ 1097 MM不哭 解题报告
嗯,看网上的题解吧,我推荐一个,觉得不错的,不贴上来了,直接放地址:
http://hi.baidu.com/qq953892596/blog/item/4c17eb807c98c8b20cf4d202.html
代码:
#include <stdio.h> #include <stdlib.h> #define min(a, b) ((a)<(b)?(a):(b)) int left[1001][1001], right[1001][1001]; int w[1001][1001]; struct girl{ int d, w; }girls[1001]; int n; void srch_r(int a, int b); void srch_l(int a, int b) { if(a - 1 >= 1 && left[a - 1][b] > left[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[a].d - girls[a - 1].d)){ left[a - 1][b] = left[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[a].d - girls[a - 1].d); srch_l(a - 1, b); } if(b + 1 <= n && right[a][b + 1] > left[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[b + 1].d - girls[a].d)){ right[a][b + 1] = left[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[b + 1].d - girls[a].d); srch_r(a, b + 1); } } void srch_r(int a, int b) { if(a - 1 >= 1 && left[a - 1][b] > right[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[b].d - girls[a - 1].d)){ left[a - 1][b] = right[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[b].d - girls[a - 1].d); srch_l(a - 1, b); } if(b + 1 <= n && right[a][b + 1] > right[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[b + 1].d - girls[b].d)){ right[a][b + 1] = right[a][b] + (w[1][a - 1] + w[b + 1][n]) * (girls[b + 1].d - girls[b].d); srch_r(a, b + 1); } } int main(int argc, char **argv) { int i, j; int s; scanf("%d%d", &n, &s); for(i = 1; i <= n; i++){ scanf("%d%d", &girls[i].d, &girls[i].w); } for(i = 1; i <= n; i++){ for(j = i; j <= n; j++){ right[i][j] = left[i][j] = 0xFFFFFFF; w[i][j] = w[i][j - 1] + girls[j].w; } } left[s][s] = right[s][s] = 0; srch_l(s, s); srch_r(s, s); printf("%d\n", min(left[1][n], right[1][n])); return 0; }