Title Link: http://poj.org/problem?id=1485
For the time being I haven't figured out a way to find it. hahaha, write the middle steps first.
#include <stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;intd[ About];///along the highway n the location of the fast food restaurantsintcost[ About][ About];///Cost[i][j] represents the establishment of a warehouse between any two fast food restaurants, and the distance of all restaurants to the warehouseintdis[ About][ About];///Dis[i][j] Represents the minimum distance between the front I warehouse and the first J fast food restaurantintn,k;intMain () { while(SCANF ("%d%d", &n,&k), n!=0|| k!=0) { for(intI=1; i<=n;i++) scanf ("%d",&D[i]); memset (Cost,0,sizeof(cost)); for(intI=1; i<=n;i++) { for(intj=1; j<=n;j++) { intTMP = (i+j)/2; for(intm=i;m<=j;m++) Cost[i][j]+=abs (d[m]-d[tmp]); }} memset (Dis,1,sizeof(DIS)); for(intI=1; i<=n;i++) dis[1][i] = cost[1][i]; for(intI=2; i<=k;i++)///Warehouse { for(intj=i;j<=n;j++)///fast food { for(intm=i-1; m<j;m++) { intTMP = dis[i-1][M] + cost[m+1][j]; if(tmp<Dis[i][j]) dis[i][j]=tmp; }}} printf ("%d\n", Dis[k][n]); } return 0;}
Store between fast food shops, shortest road, DP,POJ (1485)